【发布时间】:2013-09-02 17:04:18
【问题描述】:
我设法为以下更新方法编写了一个测试:
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 它应该通过 - 这可以在第二个测试中看到
有什么想法吗?测试应该通过,但是输入的内容的有效性应该是假的。
【问题讨论】:
-
如果你使用
update_attributes!异常会在错误时引发,所以你可以编写一个测试来检查是否引发了异常。 (我不再使用 RSpec,所以我不记得要使用的测试助手的名称)
标签: ruby-on-rails rspec tdd bdd