渲染“编辑”页面测试不起作用..不确定还有什么要尝试

时间:2011-05-29 03:34:49

标签: ruby-on-rails rspec

我试图让我的一个测试通过,但我似乎无法得到它:

Failures:
  1) UsersController PUT 'update' failure should render the 'edit' page
     Failure/Error: put :udpate, :id => @user, :user => @attr
     No route matches {:id=>#<User id: 1, name: "User_Name", email: "USER@gmail.com", created_at: "2011-05-29 03:26:30", updated_at: "2011-05-29 03:26:30", encrypted_password: "fc70fcb4b094b388d87c5054ed9b0bfa06f53431d44c527e852...", salt: nil>, :user=>{:email=>"", :name=>"", :password=>"", :password_confirmation=>""}, :controller=>"users", :action=>"udpate"}
     # ./spec/controllers/users_controller_spec.rb:164:in `block (4 levels) in <top (required)>'

这是我试图在我的UsersController中传递的代码:

def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated."
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
    end
  end

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

你的测试应该是这样的......

describe "PUT 'update'" do
before do
  @user = Factory(:user)
  test_sign_in(@user)
end

describe "failure" do
  before do
    @attr = { :name => "", :email => "", :password => "", :password_confirmation => "" }
  end

  it "should render the 'edit' page" do
    put :update, :id => @user, :user => @attr
    response.should render_template('users/edit')
  end

  it "should have the right title" do
    put :update, :id => @user, :user => @attr
    response.should have_selector("title", :content => "Edit user")
  end

end

describe "success" do
  before do
    @attr = { :name => "Other User", :email => "other_user@example.com", :password => "barfoo", :password_confirmation => "barfoo" }
  end

  it "should change the users attributes" do
    put :update, :id => @user, :users => @attr
    user = assigns(:user)
    @user.reload
    @user.name.should == user.name
    @user.email.should == user.email
    @user.encrypted_password == user.encrypted_password
  end

  it "should have a flash message" do
    put :update, :id => @user, :user => @attr
    flash[:success].should =~ /updated/
  end

end