我设法为以下更新方法编写了一个测试:
def update
@user = User.find(params[:id])
@user.update_attributes!(user_update_params)
render :nothing => true
end
context "update user" do
it "should update a user based on valid info" do
@factory = FactoryGirl.create(:user)
put :update, :id => @factory.id, :user => {
:name => 'sample_user', :user_name => 'sample_user',
:email => 'sample@gmail.com', :email_confirmation => 'sample@gmail.com',
:password => 'SamplePassword', :password_confirmation => 'SamplePassword',
:bio => 'apple sauce', :picture_url => 'http://google.ca'
}
assert_equal "sample_user", assigns(:user).name
end
it "should not update a user based on invalid info" do
@factory = FactoryGirl.create(:user)
put :update, :id => @factory.id, :user => {
:name => 'sample_user', :user_name => 'sample_user',
:email => 'sample@gmail.com', :email_confirmation => 'sample@gmail.com',
:password => 'SamplePassword', :password_confirmation => 'asdasdasd',
:bio => 'apple sauce', :picture_url => 'http://google.ca'
}
assigns(:user).valid?.should == false
end
end
但我不确定如何写出与此相反的内容。我的意思是我可以做:password_confirmation => 'asdasd'
但是当我尝试时 - 它表明我的密码确认与密码不匹配然后测试失败0应该通过 - 这可以在第二次测试中看到< /强>
有什么想法吗?测试应该通过,但内容的有效性应该是假的。
答案 0 :(得分:1)
您是否尝试获取记录并测试其当前属性?就像测试一样,如果它仍然有默认的attrs并且没有正确更新。
像
这样的东西it "should not update a user based on invalid info" do
@factory = FactoryGirl.create(:user)
put :update, :id => @factory.id, :user => {
:name => 'sample_user', :user_name => 'sample_user',
:email => 'sample@gmail.com', :email_confirmation => 'sample@gmail.com',
:password => 'SamplePassword', :password_confirmation => 'asdasdasd',
:bio => 'apple sauce', :picture_url => 'http://google.ca'
}
User.first.name.should == 'the factory default name'
User.first.name.should_not == 'sample_user'
end
答案 1 :(得分:0)
首先,您不测试控制器规格中的任何验证。这是模型规范的目的。测试关联和验证的一种好方法是使用shoulda-matchers gem。
回到你的规格;查看您的update
方法,我将规范编写为:
User
id
给定DB
时处理案例
user
实例想要使用无效参数更新时处理案例。我通常会注意具有指定属性的方法调用和实例变量的赋值。
希望它有所帮助。
context "update user" do
let(:user_id) { 63 }
let(:user) { FactoryGirl.create :user, id: user_id }
context 'when success' do
# simplified valid attributes hash
let(:valid_attributes) { { :name => user_name } }
let(:user_name) { 'sample_user' }
it 'should find the user by id passed in params' do
User.should_receive(:find).with(user_id).and_return(user)
put :update, :id => user_id, :user => valid_attributes
assigns(:user).should eq(user)
end
it "should update a user based on valid info" do
put :update, :id => user_id, :user => valid_attributes
expect(assigns(:user).name).to eq(user_name)
end
end
context 'when failure' do
let(:invalid_attributes) { {} }
context 'when a user is not found' do
let(:user_id) { nil }
it 'raises ActiveRecord::RecordNotFound exception when no user is found'
User.should_receive(:find).with(user_id).and_raise(ActiveRecord::RecordNotFound)
put :update, :id => user_id, :user => invalid_attributes
end
end
context 'when a user is found' do
it "raises ActiveRecord::RecordInvalid exception when updated with invalid attributes" do
User.any_instance.should_receive(:update_attributes!).with(invalid_attributes).and_raise(ActiveRecord::RecordInvalid)
put :update, :id => user_id, :user => invalid_attributes
end
end
end
end
答案 2 :(得分:0)
我解决这个问题的方式是
it "should not update a user based on invalid info" do
assert_raises(ActiveRecord::RecordInvalid) do
@factory = FactoryGirl.create(:user)
put :update, :id => @factory.id, :user => {
:name => 'sample_user', :user_name => 'sample_user',
:email => 'sample@gmail.com', :email_confirmation => 'sample@gmail.com',
:password => 'SamplePassword', :password_confirmation => 'asdasdasd',
:bio => 'apple sauce', :picture_url => 'http://google.ca'
}
end
end
这是因为 - 当密码不匹配时,我们无法向数据库提交数据。