我正在尝试使用OmniAuth和Devise编写与Twitter签入相关的集成测试。我在设置请求变量时遇到了困难。它可以在控制器测试中工作,但不能在集成测试中工作,这让我认为我没有正确配置spec helper。我已经查找过,但似乎找不到可行的解决方案。以下是我目前的代码:
# spec/integrations/session_spec.rb
require 'spec_helper'
describe "signing in" do
before do
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
visit new_user_session_path
click_link "Sign in with twitter"
end
it "should sign in the user with the authentication" do
(1+1).should == 3
end
end
在测试之前,这个规范就会产生错误,我不太确定request
变量需要在哪里初始化。错误信息如下:
Failure/Error: request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
NoMethodError:
undefined method `env' for nil:NilClass
现在我在控制器规范中使用request
变量,测试通过了,但是它没有被初始化用于集成测试。
# spec/spec_helper.rb
Dir[Rails.root.join("spec/support/*.rb")].each {|f| require f}
...
# spec/support/devise.rb
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
感谢您的帮助!