English 中文(简体)
铁路局的Ruby允许进行RSpec测试的大规模转让
原标题:Ruby on Rails allow mass assignment for an RSpec test

我用RSpec测试我的铁路应用,但随后我遇到一个问题。 因此,我希望有一个前后一致的数据库,我施加了一个限制,即某些栏目不能完全无效。

我有评注模式,评论可能是对另一项评论的答复。 更确切地说,一项评论有一个IP地址,不应予以否定。 这就是移民:

create_table :comments do |t|
  t.string :name, :limit => 20, :null => false
  t.string :comment, :limit => 8192, :null => false
  t.string :ip, :null => false
  t.integer :answer_to_comment_id
end

然后,我建立了<条码>Comment模型,只有<条码>>,即和<条码>>。

class Comment < ActiveRecord::Base
  attr_accessible :name, :comment

  belongs_to :answer_to, :class_name => "Comment", 
                         :foreign_key => "answer_to_comment_id"

  has_many :answers, :class_name => "Comment", 
                     :foreign_key => "answer_to_comment_id", 
                     :dependent => :destroy
end

www.un.org/Depts/DGACM/index_french.htm looks:

Factory.define :comment do |comment|
  comment.name    "test"
  comment.comment "test"  
  comment.ip      "0.0.0.0"
end

现在,在RSpec测试中,我有以下问题:comment_spec.rb

describe "some test" do
  before(:each) do
    @answer = @comment.answers.create(Factory.attributes_for(:comment))
  end
end

由于<代码>:ip不在attr_accessible清单之内,因此主动查询可在数据库中生成记录。 我可在名单上添加<代码>:ip,但这可能由于大规模派任而造成一些安全问题。 或可添加<代码>:ip 人工操作,但如果具备更多的特性,如<条码>,则会成为许多工作。

So I look for a possibility to bypass the attr_accessible list. Or if you have a better design pattern, please let me know

谢谢。

最佳回答

Just use:

describe "some test" do
  before(:each) do
    @answer = @comment.answers << Factory(:comment)
  end
end

或者如果需要不止一个评论的话,请参见n

describe "some test" do
  before(:each) do
    @answer = @comment.answers = FactoryGirl.create_list(:comment, n)
  end
end
问题回答

我在寻求解决同一问题的办法时,一直在处理这个问题。 我知道这很老,但就我通过守则提出的价值而言,我决定以这种方式解决这一问题:

before :each do
  ActiveModel::MassAssignmentSecurity::WhiteList.any_instance.stub(:deny?).and_return(false)
end

Perhaps this will come in handy to someone else who winds up here.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签