【问题标题】:shoulda, rspec, guard, spork issues with http responsesshoulda、rspec、guard、spork 与 http 响应有关的问题
【发布时间】:2012-03-05 23:15:38
【问题描述】:

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

它{should respond_with(:success)}

运行测试时我得到

预期响应为 200,但为 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

【问题讨论】:

  • 你没有重定向吗?
  • 目前没有。 routes.rb 只是声明了resources :plans 并有一个类似match '/pricing' =&gt; 'plans#index' 的别名。你知道我之前提到的测试是在 PlansController 类的方法上。
  • 嗯,重定向通常发生在控制器内
  • 也许您在此操作之前有一些身份验证过滤器,而您没有在规范中定义它
  • 编辑问题以显示更多代码,反正我的控制器中没有任何重定向 atm。

标签: ruby-on-rails testing rspec shoulda


【解决方案1】:

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

更新如下:

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

  it { should respond_with(:success) }
end

【讨论】:

  • before do 与 rspec 中的 before :each do 相同。只是为了方便。
猜你喜欢
  • 2011-12-31
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多