RSpec测试PUT更新操作 [英] RSpec test PUT update action

查看:93
本文介绍了RSpec测试PUT更新操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些RSpec测试来测试我的应用,但是我偶然发现了一些找不到任何解决方案的问题。
1)我正在尝试测试更新操作。这是我的代码:

I am trying to write some RSpec tests to test my app, but I've stumbled on several problems that I can't find any solution. 1) I am trying to test the update action. Here is my code:

        it "email is a new one" do
            put :update, id: @user, user: FactoryGirl.attributes_for(:user, :email=>"a@b.c")
            @user.reload
            @user.email.should == "a@b.c"
            puts @user.email
        end

这是UsersController更新操作:

Here is the UsersController update action:

  def update
    @user = User.find(params[:id])
    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to edit_user_path(@user), :notice => "Your settings were successfully updated."}
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

这是错误:

 Failure/Error: @user.email.should == "a@b.c"
       expected: "a@b.c"
            got: "user16@example.com" (using ==)

很明显,测试并没有改变用户的电子邮件。
我从此处获取了更新操作教程: http: //everydayrails.com/2012/04/07/testing-series-rspec-controllers.html
在哪里可以找到解决方案?

It's obvious that the test hasn't changed the email of the user. I took the update action tutorial from here: http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html . Where I could find the solution?

推荐答案

可以 @ user.update_attributes(params [ :user])是否由于验证原因而失败?

Could @user.update_attributes(params[:user]) be failing for a validation reason?

此外,您可以确保测试和控制器方法与之交互红宝石对象。过去对我来说是个陷阱。我这样做的方法是在类中存根 find 方法。

Also, you could ensure that your test and the controller method are interacting with the same ruby object. That's been a gotcha for me in the past. The way I do it is to stub the find method on the class.

it "email is a new one" do
  User.stubs(:find).returns(@user)
  put :update, id: @user, user: FactoryGirl.attributes_for(:user, :email=>"a@b.c")
  @user.reload
  @user.email.should == "a@b.c"
  puts @user.email
end

这可以确保您在测试过程中不仅在谈论相同的记录,而且在谈论的是同一对象。

This ensures that you're talking about not only the same record, but the same object during the test.

最后,我认为您的测试对您有很大帮助。您基本上是在测试 update_attributes ,这是一项核心功能,并且已经过全面测试。我将专注于测试控制器的行为。像这样的东西:

Lastly, I would argue that your test is doing very much for you. You're basically testing update_attributes, which is a core feature and thoroughly tested already. I would focus on testing the controller behavior. Something like this:

let(:user) { FactoryGirl.create(:user) }

describe "PUT #update" do

  before(:each) {
    User.stubs(:find).returns(user)
  }

  it "should redirect to the user path on succesful save" do
    user.should_receive(:update_attributes).and_return true
    put :update, user, {}
    response.should redirect_to(edit_user_path(user))
  end

  it "should render the edit screen again with errors if the model doesn't save" do
    user.should_receive(:update_attributes).and_return false
    put :update, user, {}
    response.should render_template("edit")
  end
end

这篇关于RSpec测试PUT更新操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆