1

我已经设置了 Rspec2 + Spork + Guard + Devise

我的文件如下

#spec_helper.rb
Spork.prefork do
  #code
  Dir[Rails.root.join('spec/support/**/*.rb')].each {|f| require f}
  RSpec.configure do |config|
    config.extend ControllerMacros, :type => :controller 
  end 
end

Spork.each_run do
  # This code will be run each time you run your specs.
  FactoryGirl.reload
  include ControllerMacros
end

#spec/support/controller_macros.rb
module ControllerMacros
    def login_user
      before(:each) do
        @request.env["devise.mapping"] = Devise.mapping[:user]
        user = FactoryGirl.create(:user)
        sign_in user
      end
    end
end

#spec/support/devise.rb
Spec.configure do |config|
    config.include Devise::TestHelpers, :type => :controller
end

在我的请求规范中

#spec/features/documents_spec.rb
require 'spec_helper'

describe "Documents" do

  login_user

  describe "GET /documents" do
    it "should display document name as sameera CV" do
      #spec code
    end
  end
end

当我跑步时bundle exec guard,我得到

  1) Documents GET /documents should display document name as sameera CV
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `env' for nil:NilClass
     # ./spec/support/controller_macros.rb:4:in `block in login_user'

到目前为止,我已经通过谷歌做了很多修复,但似乎没有任何效果,有人可以帮助我:)

我上线了

  • 导轨 3.2.9
  • rspec 2.12.0
  • 设计 2.2.3

任何帮助将不胜感激

4

2 回答 2

1

尝试更改@request.env["devise.mapping"] = Devise.mapping[:user]request.env["devise.mapping"] = Devise.mapping[:user]inspec/support/controller_macros.rb

于 2013-02-28T19:52:42.060 回答
0

在这里,我正在回答我自己的问题,并且我能够为我提出的问题找到解决方法。

以下是我所做的步骤

1)从支持目录中删除controller_macros.rbanddevise.rb

2)ControllerMacrosspec_helper.rb

3)添加以下代码

#spec/features/documents_spec.rb
before(:each) do
   user = FactoryGirl.create(:user)
   visit root_path
   fill_in 'user_email', :with => user.email
   fill_in 'user_password', :with => user.password
   click_button 'Sign in'
end

我敢肯定应该有一种更优雅的方式(如设计维基中所述),但这很有效:)

于 2013-03-01T04:31:13.740 回答