我如何获取 RSpec 响应中包含的参数来检查它?

How do I get at the params included in an RSpec response to examine it?

我正在编写控制器规范(RSpec with Devise),我知道响应 returns 是我传递给它的参数(正确),因为我在输出如下:

...@params={"email" => "maddison_stokes@schumm.org", ...

这是我的控制器规格:

  describe 'POST #create' do
    context 'when invited user IS an existing user' do
        before :each do
          @users =  [
            attributes_for(:user),
            attributes_for(:user),
            attributes_for(:user)
          ]
        end

        it 'correctly finds User record of invited user' do
          login_user
          post :create, { email: @users.first[:email] }
          expect(response.params[:email]).to include(@users.first[:email])
        end
     end
  end

当我运行上面的测试时,我得到这个错误:

  1) Users::InvitationsController POST #create when invited user IS an existing user correctly finds User record of invited user
     Failure/Error: expect(response.params[:email]).to include(@users.first[:email])
     NoMethodError:
       undefined method `params' for #<ActionController::TestResponse:0x007fa50d1ee6a0>

响应的整个输出如下所示:

https://gist.github.com/marcamillion/c2e3f4d0bdae05c2be1f

我试着把它贴在这里,但因为超过了 30,000 个字符的限制而对我大喊大叫。我会 运行 对其进行分类,但不想删除任何可能需要的信息。

我想做的基本上是验证 response 中包含的 params[:email] 中的电子邮件是否与我传递给它的电子邮件相同。我知道这是直观的,但我想以编程方式进行。

undefined method `params' for ActionController::TestResponse:0x007fa50d1ee6a0

您应该使用 controller.params[:email] 而不是 response.params[:email]

params 绑定到 controller 而不是 response