我将如何使用 Rspec 测试此控制器操作

发布于 2024-12-04 16:45:51 字数 1218 浏览 2 评论 0原文

我应该如何指定这个

class FlagsController
  def like
    flag = current_user.flags.find_or_initialize_by_flaggable_type_and_flaggable_id(params[:like_type], params[:like_id])
    flag.kind = params[:kind]

    if flag.save
      head :ok
    else
      head :unprocessable_entity
    end
  end
end

我当前的规格

describe FlagsController do
    before (:each) do
      @user = Factory(:user)
      sign_in @user
    end

    describe "flag" do
      it 'with valid params' do
        controller.stub!(:current_user).and_return(@user)
        @user.stub_chain(:flags, :find_or_initialize_by_flaggable_type_and_flaggable_id).
          with('Comment', '1').
          and_return(mock_flag(:save => false)).
          stub!(:kind).
          with('up').
          and_return(mock_flag(:save => true))
        post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
        response.response_code.should eq(200)
      end
    end

规格结果

Failure/Error: response.response_code.should eq(200)

   expected 200
        got 422

   (compared using ==)

路线

post 'like' => 'flags#like'

How should I spec this

class FlagsController
  def like
    flag = current_user.flags.find_or_initialize_by_flaggable_type_and_flaggable_id(params[:like_type], params[:like_id])
    flag.kind = params[:kind]

    if flag.save
      head :ok
    else
      head :unprocessable_entity
    end
  end
end

My current spec

describe FlagsController do
    before (:each) do
      @user = Factory(:user)
      sign_in @user
    end

    describe "flag" do
      it 'with valid params' do
        controller.stub!(:current_user).and_return(@user)
        @user.stub_chain(:flags, :find_or_initialize_by_flaggable_type_and_flaggable_id).
          with('Comment', '1').
          and_return(mock_flag(:save => false)).
          stub!(:kind).
          with('up').
          and_return(mock_flag(:save => true))
        post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
        response.response_code.should eq(200)
      end
    end

Spec results

Failure/Error: response.response_code.should eq(200)

   expected 200
        got 422

   (compared using ==)

Route

post 'like' => 'flags#like'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-12-11 16:45:52

我将如何编写这个测试:

describe FlagsController do
  before (:each) do
    @user = Factory(:user)
    sign_in @user
  end

  describe "post :like" do
    before(:each) do
      controller.stub!(:current_user).and_return(@user)
      @mock_flag = mock(Flag)
      @mock_flag.should_receive(:kind=).with('up')
      @user.stub_chain(:flags, :find_or_initialize_by_flaggable_type_and_flaggable_id).
            with('Comment', '1').and_return(@mock_flag)
    end
    context 'when save fails' do
      before(:each) do
        @mock_flag.should_receive(:save).and_return(false)
        post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
      end

      it { response.should be_unprocessable_entity }
    end
    context 'when save succeeds' do
      before(:each) do
        @mock_flag.should_receive(:save).and_return(true)
        post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
      end

      it { response.status.should be == 200 }
    end
  end

希望这会有所帮助。

How I would write this test:

describe FlagsController do
  before (:each) do
    @user = Factory(:user)
    sign_in @user
  end

  describe "post :like" do
    before(:each) do
      controller.stub!(:current_user).and_return(@user)
      @mock_flag = mock(Flag)
      @mock_flag.should_receive(:kind=).with('up')
      @user.stub_chain(:flags, :find_or_initialize_by_flaggable_type_and_flaggable_id).
            with('Comment', '1').and_return(@mock_flag)
    end
    context 'when save fails' do
      before(:each) do
        @mock_flag.should_receive(:save).and_return(false)
        post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
      end

      it { response.should be_unprocessable_entity }
    end
    context 'when save succeeds' do
      before(:each) do
        @mock_flag.should_receive(:save).and_return(true)
        post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
      end

      it { response.status.should be == 200 }
    end
  end

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文