我设法为以下更新方法编写了一个测试:
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 它应该通过 -这可以在第二个测试中看到
有任何想法吗?测试应该通过,但是输入内容的有效性应该是错误的。