RSpec控制器宏不工作

时间:2013-03-18 14:59:02

标签: ruby-on-rails rspec

Rails 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'用于#(NameError)

2 个答案:

答案 0 :(得分:2)

似乎已经回答here,您需要将extend更改为include

答案 1 :(得分:2)

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

在:

describe Api::FooController do
   .
   .
end

后:

describe Api::FooController, type: :controller do
   .
   .
end