开发者

Help understanding this RSpec test

开发者 https://www.devze.com 2023-02-25 18:07 出处:网络
I\'m trying to firm up my understanding of why this actually works My application is just the default scaffold for 开发者_如何学Cthe purposes of this experiment.

I'm trying to firm up my understanding of why this actually works My application is just the default scaffold for 开发者_如何学Cthe purposes of this experiment.

it "creates a record and redirects with success notice" do
    Project.any_instance.stubs(:valid?).returns(true)   #ensure action behaves as if @project was valid
    post :create, :format => :js
    assigns(:project).errors.should be_empty    #ensures that @project has no errors
    flash[:notice].should_not be_nil            #ensure a flash notice is set
    response.should redirect_to(projects_path)  #ensure appropriate redirection
  end

end

In the controller the save method is called on @project. Here is where I become unsure. The object will save because the valid? method has been stubbed to return true, even if the object is not valid. But if the object is not valid how can the save be successful?


Rails is also calling your valid? stub internally before saving, so it proceeds with the save as normal because it thinks nothing is wrong.

On the other hand if you had, say, a UNIQUE index on a field in a MySQL database, a duplicate row there could still prevent the record from being saved, even though you stubbed valid?. Two possible layers of validation.

0

精彩评论

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