传递变量进行测试时失败的规范

时间:2016-07-05 12:31:40

标签: ruby-on-rails ruby rspec

这是我的规范文件:

require 'rails_helper'

RSpec.describe Programmes::ReportsController, :type => :controller do
  let!(:programme) { create(:programme) }
  context 'authenticated user' do
    describe 'GET index' do
      it 'responds with a 200 OK status code' do
        get :index, params: { id: programme.id }
        expect(response).to have_http_status(:success)
      end
    end
  end
end

这是我的工厂;

FactoryGirl.define do
  factory :programme do
    name { Faker::Lorem.word }
    description { Faker::Lorem.sentence(3) }
  end
end

这是我的控制器;

class Programmes::ReportsController < ApplicationController
  def index
  end

  def create
  end 
end

我似乎无法通过此规范。该路由在浏览器中正常工作;例如

http://localhost:3000/programmes/{:id}/reports

我遇到的错误是:

Failures:
1) Programmes::ReportsController authenticated user GET index responds with a 200 OK status code
   Failure/Error: let!(:programme) { create(:programme) }
   NoMethodError:
     undefined method `create' for #<RSpec::ExampleGroups::ProgrammesReportsController::AuthenticatedUser::GETIndex:0x007fac78b1b440>
   # /Users/mike/.rvm/gems/ruby-2.2.3/gems/actionpack-5.0.0/lib/action_dispatch/testing/assertions/routing.rb:172:in `method_missing'

我是Ruby(和Rails)的新手。我不认为在FactoryGirl中创建了Program对象 - 但我真的不知道如何找出是否是这样的

1 个答案:

答案 0 :(得分:0)

您在spec_helper中有require 'factory_girl'吗?