0

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

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

有任何想法吗?测试应该通过,但是输入内容的有效性应该是错误的。

4

3 回答 3

1

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

就像是

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
于 2013-09-02T19:57:36.827 回答
0

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

所以回到你的规格;看看你的update方法,我会把规范写成:

  1. 先写个happy path的例子,(用户被找到且参数有效)
  2. 处理在,中找不到Userwith given的情况idDB
  3. 处理user实例想要使用无效参数更新的情况。

我通常关注具有指定属性的方法调用和实例变量的分配。

希望能帮助到你。


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
于 2013-09-02T18:56:00.360 回答
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

这通过了,因为 - 当密码不匹配时,我们无法将数据提交到数据库。

于 2013-09-02T22:54:21.767 回答