我有以下
it 'should assign a new profile to user' do
get :new
assigns(:user_profile).should ==(Profile.new)
end
但它不起作用。我试过'eql?' 和“平等?” 分别。我如何比较它才能知道@user_profile 的内容是否是Profile.new?
我曾经做过一个变通方法,对分配的变量进行 .class 处理,检查它是否是 Profile,但我想停止这些不良做法。
谢谢。
我有以下
it 'should assign a new profile to user' do
get :new
assigns(:user_profile).should ==(Profile.new)
end
但它不起作用。我试过'eql?' 和“平等?” 分别。我如何比较它才能知道@user_profile 的内容是否是Profile.new?
我曾经做过一个变通方法,对分配的变量进行 .class 处理,检查它是否是 Profile,但我想停止这些不良做法。
谢谢。
这里的问题是,Object.new设计调用两次会创建两个不同的对象,它们不相等。
1.9.2p318 :001 > Object.new == Object.new
=> false
你可以在这里做的一件事是
let(:profile){ Profile.new }
it 'should assign a new profile to user' do
Profile.should_receive(:new).and_return profile
get :new
assigns(:user_profile).should eq profile
end
现在,当调用控制器操作时,您实际上并没有创建新的配置文件,但您仍在测试Profile正在接收new,并且您正在测试该方法的返回值是否由控制器分配给@user_profile.