Rspec 获得;未定义的方法'env';对于nil:NilClass“;OmniAuth的集成测试

Rspec 获得;未定义的方法'env';对于nil:NilClass“;OmniAuth的集成测试,rspec,omniauth,Rspec,Omniauth,我在以下位置设置了Omniauth测试模式: spec\u helper(我把它放在文件的底部,就在end之前): #Turn on "test mode" for OmniAuth OmniAuth.config.test_mode = true describe "signin" do before { visit signin_path } . . . describe "with OmniAuth" do before do

我在以下位置设置了Omniauth测试模式:

spec\u helper(我把它放在文件的底部,就在
end
之前):

#Turn on "test mode" for OmniAuth 
OmniAuth.config.test_mode = true
 describe "signin" do
    before { visit signin_path }
    .
    .
    .
    describe "with OmniAuth" do
      before do
        OmniAuth.config.add_mock :facebook, uid: "fb-12345", info: { name: "Bob Smith" }
        visit root_path
      end

      describe "Facebook provider" do
        before do
          request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
          click_link "Sign in with Facebook"
        end

        it { should have_selector('title', text: user.name) }

        it { should have_link('Users',    href: users_path) }
        it { should have_link('Profile',  href: user_path(user)) }
        it { should have_link('Settings', href: edit_user_path(user)) }
        it { should have_link('Sign out', href: signout_path) }

        it { should_not have_link('Sign in', href: signin_path) }
      end
这是我的测试:

规范/请求/授权\u页面\u规范rb:

#Turn on "test mode" for OmniAuth 
OmniAuth.config.test_mode = true
 describe "signin" do
    before { visit signin_path }
    .
    .
    .
    describe "with OmniAuth" do
      before do
        OmniAuth.config.add_mock :facebook, uid: "fb-12345", info: { name: "Bob Smith" }
        visit root_path
      end

      describe "Facebook provider" do
        before do
          request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
          click_link "Sign in with Facebook"
        end

        it { should have_selector('title', text: user.name) }

        it { should have_link('Users',    href: users_path) }
        it { should have_link('Profile',  href: user_path(user)) }
        it { should have_link('Settings', href: edit_user_path(user)) }
        it { should have_link('Sign out', href: signout_path) }

        it { should_not have_link('Sign in', href: signin_path) }
      end
当我运行测试时,我得到以下信息:

失败:

  1) Authentication signin with OmniAuth Facebook provider 
     Failure/Error: request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
     NoMethodError:
       undefined method `env' for nil:NilClass
     # ./spec/requests/authentication_pages_spec.rb:57:in `block (5 levels) in <top (required)>'
1)使用OmniAuth Facebook提供商进行身份验证登录
失败/错误:request.env[“omniauth.auth”]=omniauth.config.mock_auth[:facebook]
命名错误:
nil:NilClass的未定义方法“env”
#./spec/requests/authentication\u pages\u spec.rb:57:in'block(5级)in'
(等等)


是缺少了什么还是我做错了什么?

我认为您在这个示例中混合了两种不同的东西。您正在尝试编写集成规范,但
请求
方法AFAIK仅在控制器规范中可用(适用于
规范/控制器
目录中的规范)。因此,对于nil:NilClass`错误,您有
未定义的方法
env'

您可以在我的示例项目中找到健康的OmniAuth集成规范:


我希望这对你有帮助。

你明白了吗?我也有同样的问题……@Brandon两周没有睡觉后,我放弃了。并采用了一条新规则:永远不要测试gems,否则你会讨厌测试。这不是一个功能规范,而不是一个请求规范吗?