ruby-on-rails - shoulda、rspec、guard、spork 与 http 响应有关的问题

标签 ruby-on-rails testing rspec shoulda

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

it {should respond_with(:success)}

运行测试时我得到

Expected response to be a 200, but was 301

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

编辑:更多来源

Controller :

 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

最佳答案

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

更新如下:

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

  it { should respond_with(:success) }
end

关于ruby-on-rails - shoulda、rspec、guard、spork 与 http 响应有关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9279398/

相关文章:

rspec - 当第一个页面与第二个页面具有相同的字段时,等待第二个页面加载 Capybara 的正确方法

testing - 什么是测试线束?

testing - 功能测试 : SIKULI doesn't work with basic Google Chrome

javascript - 未使用的 css - 你如何清理它?

ruby-on-rails - rails : difference between <%= and <%==?

testing - Meteor.js CircleCI 集成在 linting 上失败

ruby-on-rails - Rspec:在模型无效时显示模型的错误

ruby-on-rails - Rspec 测试失败缺少模板格式问题

jquery - Rails 如何在不涉及表单的情况下在 Ajax 请求中发送真实性 token ?

ruby-on-rails - 我如何检查类或 json 对象中的所有属性/值是否为 nil