1

我正在用 capybara 测试我的自定义设计视图,我需要一个登录用户来测试一些视图,例如:

feature 'user/edit' do
  describe "the edit profile process", :type => :feature do
    include Devise::TestHelpers

    let!(:user){ FactoryGirl.build(:user)

    def sign_in(user)
      if user.nil?
        request.env['warden'].stub(:authenticate!).
          and_throw(:warden, {:scope => :user})
          controller.stub :current_user => nil
      else
        request.env['warden'].stub :authenticate! => user
         controller.stub :current_user => user
      end
    end

    it "update " do
      sign_in(user)
      visit '/users/edit'
      within("#edit_user") do  
         #=> something
      end
      click_on 'Update'
      page.should have_content("something")
    end
  end
end

但我得到了这个错误

Failure/Error: request.env['warden'].stub :authenticate! => user

NoMethodError: undefined method `env' for nil:NilClass
4

1 回答 1

1

模块Devise::TestHelpers建立@request实例变量。您正在引用request,这显然设置为nil在其他一些可访问的范围内,从而导致您遇到错误。

有关. _ _TestHelpers

于 2013-10-30T16:40:20.287 回答