开发者

Trouble on using multiple class object instances?

I am using Ruby on Rails 3.0.9 and RSpect 2. I am trying to refactoring some spec file in the following way (in order to test with less code similar User class object attribute values):

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') }

[ user1, user2, user3 ].each do |user|
  ...
end

However, if I run the above test I get the following error:

undefined local variable or method `user1' for #<Class:0x00000103ddb158> (NameError)

What is the problem? How can I solve that?开发者_运维技巧


You need your example code inside of a "it" block

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 "should be whatever" do

    [ user1, user2, user3 ].each do |user|
      ...    
    end

  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜