RSPEC测试请求不执行控制器/操作

发布于 2025-02-03 08:58:38 字数 576 浏览 1 评论 0原文

我有此RSPEC测试:

RSpec.describe 'Navbar', type: :request do
  describe 'GET /close_navbar' do
    it 'returns http success' do
      get '/close_navbar'
      expect(response).to have_http_status(:ok)
    end
  end
end

测试控制器/操作:

class Navbar < ApplicationController
  def close_navbar
    raise 'closing'
    session[:navbar] = :closed
    head :ok
  end
end

我需要它来测试会话是否具有适当的值进行更新。

但是我不认为该操作正在执行,特别是我添加了rise行以查看,并且没有例外。

此外,测试失败了,因为返回的HTTP状态为:找到,而不是:ok

I have this rspec test:

RSpec.describe 'Navbar', type: :request do
  describe 'GET /close_navbar' do
    it 'returns http success' do
      get '/close_navbar'
      expect(response).to have_http_status(:ok)
    end
  end
end

testing a controller/action:

class Navbar < ApplicationController
  def close_navbar
    raise 'closing'
    session[:navbar] = :closed
    head :ok
  end
end

I need this to test that the session is updated with the proper value.

but I don't think the action is being executed, in particular I added the raise line to see, and no exception is raised.

also the test fails because the returned http status is :found, not :ok

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

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

发布评论

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

评论(1

不爱素颜 2025-02-10 08:58:38

在检查响应主体时,我认为该应用程序正在将我重定向到Sign_in Action,因此这就是为什么我的动作未执行的原因。

那是正确的行为,我有测试帮助者可以在测试中登录用户。所以我使用了这些,它起作用了。

checking the response body, I figured that the app was redirecting me to sign_in action, so that's why my action wasn't executed.

that is the correct behaviour, and I have test helpers to sign in a user in the tests. so I used these and it worked.

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