我使用Ruby on Rails的3.0.9和RSpect 2.我想通过以下方式重构了一些规范文件(为了用更少的代码相似User
类对象来测试属性值):指定明确的'主题?
describe User do
let(:user1) { Factory(:user, :users_attribute_a => 'invalid_value') }
let(:user2) { Factory(:user, :users_attribute_b => 'invalid_value') }
let(:user3) { Factory(:user, :users_attribute_c => 'invalid_value') }
it "foreach user" do
[ user1, user2, user3 ].each do |user|
subject { user }
it "should be whatever"
user.should_not be_valid
...
end
end
end
end
但是,如果我运行上面的测试中,我得到以下错误:
Failure/Error: it "should be whatever" do
NoMethodError:
undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_2:0x00000106ccee60>
问题是什么?我该如何解决这个问题?
的@Emily答案后更新。
如果在上面的代码中,我使用的context "foreach user" do ...
代替it "foreach user" do ...
我得到以下错误:
undefined local variable or method `user1' for #<Class:0x00000105310758> (NameError)
仅供参考,使用'subject'是一个红鲱鱼在这里。你从来没有真正使用你在你的例子中设置的主题。 – Emily