我正在使用RSpec测试我的Rails应用程序,但后来遇到了问题。我想要一个一致的数据库,因此我强加了一些约束,某些列不能为空。Ruby on Rails允许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
模型只name
和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
我factories.rb
看起来是这样的:
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
列表中,因此ActiveRecord无法在数据库中创建记录。我可以将:ip
添加到列表中,但由于批量分配,这可能会导致一些安全问题。我也可以手动添加的:ip
,但如果有像ip
所以我找了可能绕过attr_accessible
列表的详细属性,这可能会成为一个大量的工作。或者,如果你有更好的设计模式,请让我知道
谢谢
如果您使用'create!',该怎么办? – apneadiving