shoulda、rspec、guard、spork 的 http 响应问题

发布于 2025-01-06 06:01:07 字数 851 浏览 1 评论 0原文

您好,感谢您的耐心等待! 我的 Rails 应用程序使用 rspec 和 Shoulda 的组合来运行测试。测试是自动进行的,overguard 和 spork。我的一个控制器测试看起来像

它{应该respond_with(:success)}

运行测试时我得到

预期响应为 200,但实际响应为 301

通过浏览和手动测试得到 301。 wget 一切正常,页面正确响应,状态代码为 200。由于我对 Rails 测试相当陌生,因此我可能不了解测试当前是如何运行的。它们是如何实施的?环境“测试”的目的是什么?是否有某种网络服务器在后台运行来运行测试?显然存在某种不需要的重定向。 提前致谢!

编辑:更多来源

控制器:

 class PlansController < ApplicationController
   def index
     @plans=Plan.all
   end
    ... more methods ...
 end

测试:

  describe PlansController do
    before :each do
      @plan=FactoryGirl.create(:plan)
    end
    
    context " get :index" do
      before do 
        get :index 
      end

      it {should respond_with(:success)}
    end
    ... more tests..
  end

Hello and thanks for you patience!
My rails app uses a combination of rspec and shoulda to run tests. The tests are automated over guard and spork. One of my controllers tests looks like

it {should respond_with(:success)}

When running tests i get

Expected response to be a 200, but was 301

manually testing by browsing & wget things go right, the page is responding correctly with 200 status code. As I am quite new to rails testing, proberly I am not understanding how the tests are currently ran. How are they implemented? What was the purpose of the environment 'test'? Is there some kind of webserver running in backgroud to run the tests? Obviously there is some kind of non-wanted redirecting.
Thanks in advance!

Edit: More sources

controller:

 class PlansController < ApplicationController
   def index
     @plans=Plan.all
   end
    ... more methods ...
 end

test:

  describe PlansController do
    before :each do
      @plan=FactoryGirl.create(:plan)
    end
    
    context " get :index" do
      before do 
        get :index 
      end

      it {should respond_with(:success)}
    end
    ... more tests..
  end

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

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

发布评论

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

评论(1

不羁少年 2025-01-13 06:01:07

您在 get :index 上下文的 before 块中缺少 :each,因此您永远不会调用索引操作。

更新如下:

context " get :index" do
  before(:each) do 
    get :index 
  end

  it { should respond_with(:success) }
end

You are missing an :each in the before block of the context for get :index so you are never calling the index action.

Update as follows:

context " get :index" do
  before(:each) do 
    get :index 
  end

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