当我想测试属性是否是/非可访问的RSpec时,我会这样做
class Foo
attr_accesible :something_else
end
describe Foo do
it('author should not be accessible') {lambda{described_class.new(:author=>true)}.should raise_error ActiveModel::MassAssignmentSecurity::Error}
it('something_else should be accessible'){lambda{described_class.new(:something_else=>true)}.should_not raise_error ActiveModel::MassAssignmentSecurity::Error}
end有更好的办法吗?
...thx
发布于 2012-08-07 15:51:07
在Rails教程中,属性可访问性测试就是这样完成的。,我觉得挺不错的。因此,在您的示例中,可以稍微修改测试代码,使其读起来如下:
describe Foo do
describe "accessible attributes" do
it "should not allow access to author" do
expect do
Foo.new(author: true)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
it "should allow access to something_else" do
expect do
Foo.new(something_else: true)
end.to_not raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
end如果这不是你想要的,当你问你是否有“更好的方法”时,你能给我们一个更好的答案吗?
编辑
您可能对肩ActiveModel匹配器感兴趣,它会将代码清理到每测试一行。类似于:
it { should_not allow_mass_assignment_of(:author) }
it { should allow_mass_assignment_of(:something_else) }https://stackoverflow.com/questions/11775084
复制相似问题