【问题标题】:rspec test of index, show all not archivedrspec 测试索引,显示所有未归档
【发布时间】:2016-03-20 14:10:58
【问题描述】:

我正在尝试为我要实现的新功能设置(基本)测试。我有一个作业控制器,而不是默认显示所有作业,我喜欢隐藏所有已归档的作业。我尝试了不同的方法,但似乎我错过了这个难题的一两个。首先,我尝试调用“访问”,但得到它不存在的消息。第二种方法是使用“渲染”,但这也会导致错误提示渲染不存在。 (我什至可以在控制器规范中使用这些方法吗?)

把它放在控制器测试中是错误的吗?

2 最后一次测试导致错误

require "rails_helper"
require "spec_helper"


describe JobsController, :type => :controller do 

  context 'GET index' do   
   it "should be successful" do
     get :index
     expect(response).to be_success
   end

  it "renders the index template" do
   get :index
   expect(response).to render_template("index")
 end

 it "should not show any archived jobs as default" do
    visit jobs_path
    page.should have_no_content("Archived")

 end

  it 'should show the headers' do
    render :template => 'job/index', :layout => 'layouts/job'
    rendered.should_not contain('Archived')

  end

 end
end

【问题讨论】:

    标签: ruby-on-rails-3 rspec capybara rspec-rails


    【解决方案1】:

    Capybara 用于功能规范,其匹配器可用于视图规范。
    默认情况下,控制器规范实际上并不渲染视图,因为它们是检查页面内容的错误位置 - https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs 您可能应该将一些测试移至功能测试/查看测试

    【讨论】:

      猜你喜欢
      • 2016-07-25
      • 2013-08-18
      • 1970-01-01
      • 2021-04-30
      • 2023-03-27
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      相关资源
      最近更新 更多