7

导轨 3.2,RSpec 2.11。控制器宏不起作用,从我所做的所有研究来看,它似乎是正确编写的。这是代码

/spec/support/controller_macros.rb

module ControllerMacros
   def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = FactoryGirl.create(:user)
      @current_user = user
      sign_in user
    end
  end
end

/spec/spec_helper.rb

RSpec.configure do |config|
  ....
  config.extend ControllerMacros, :type => :controller
end

/spec/controllers/companies_controller_spec.rb

require File.dirname(__FILE__) + '/../spec_helper'

describe CompaniesController, "index companies" do
    context "for authenticated users" do
        login_user
        ...
    end
end

执行结果:未定义的局部变量或方法 'login_user' for # (NameError)

4

2 回答 2

2

似乎已经在这里回答了,您需要将您的更改extendinclude

于 2013-03-18T15:09:24.097 回答
2

添加规范类型为我修复了它:

前:

describe Api::FooController do
   .
   .
end

后:

describe Api::FooController, type: :controller do
   .
   .
end
于 2015-02-04T04:19:05.683 回答