如何在rspec中更新失败?

时间:2013-09-02 17:04:18

标签: ruby-on-rails rspec tdd bdd

我设法为以下更新方法编写了一个测试:

  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应该通过 - 这可以在第二次测试中看到< /强>

有什么想法吗?测试应该通过,但内容的有效性应该是假的。

3 个答案:

答案 0 :(得分:1)

您是否尝试获取记录并测试其当前属性?就像测试一样,如果它仍然有默认的attrs并且没有正确更新。

这样的东西
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'
  }
  User.first.name.should == 'the factory default name'
  User.first.name.should_not == 'sample_user'
end

答案 1 :(得分:0)

首先,您不测试控制器规格中的任何验证。这是模型规范的目的。测试关联和验证的一种好方法是使用shoulda-matchers gem。

回到你的规格;查看您的update方法,我将规范编写为:

  1. 首先编写快乐路径的示例,(找到用户并且参数有效)
  2. User
  3. 中找不到id给定DB时处理案例
  4. user实例想要使用无效参数更新时处理案例。
  5. 我通常会注意具有指定属性的方法调用和实例变量的赋值。

    希望它有所帮助。

    
    context "update user" do
      let(:user_id) { 63 }
      let(:user)    { FactoryGirl.create :user, id: user_id }
    
      context 'when success' do
        # simplified valid attributes hash
        let(:valid_attributes) { { :name => user_name } }
        let(:user_name) { 'sample_user' }
    
        it 'should find the user by id passed in params' do
          User.should_receive(:find).with(user_id).and_return(user)
    
          put :update, :id => user_id, :user => valid_attributes
    
          assigns(:user).should eq(user)
        end
    
        it "should update a user based on valid info" do
          put :update, :id => user_id, :user => valid_attributes
    
          expect(assigns(:user).name).to eq(user_name)
        end
      end
    
      context 'when failure' do
        let(:invalid_attributes) { {} }
    
        context 'when a user is not found' do
          let(:user_id) { nil }
    
          it 'raises ActiveRecord::RecordNotFound exception when no user is found'
            User.should_receive(:find).with(user_id).and_raise(ActiveRecord::RecordNotFound)
    
            put :update, :id => user_id, :user => invalid_attributes
          end
        end
    
        context 'when a user is found' do
          it "raises ActiveRecord::RecordInvalid exception when updated with invalid attributes" do
            User.any_instance.should_receive(:update_attributes!).with(invalid_attributes).and_raise(ActiveRecord::RecordInvalid)
    
            put :update, :id => user_id, :user => invalid_attributes
          end
        end
      end
    end
    

答案 2 :(得分:0)

我解决这个问题的方式是

it "should not update a user based on invalid info" do
  assert_raises(ActiveRecord::RecordInvalid) 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'
    }
  end
end

这是因为 - 当密码不匹配时,我们无法向数据库提交数据。