Oauth模型关注测试覆盖范围存根 [英] Oauth Model Concern Test Coverage Stubs
问题描述
我正在尝试找出达到该课程100%测试覆盖率的最佳方法.我已经概述了我的完整规格,希望有人可以指出正确的方向.我的假设是对Oauth2请求进行存根可以做到这一点,但我似乎无法实现这一目标.
I am trying to figure out the best way to reach 100% test coverage on this class. I have outlined my full spec and I am hoping someone can point me in the right direction. My assumption is stubbing the Oauth2 request would do this, but I can not seem to make that work.
我正在使用Rails 4.
I'm using Rails 4.
规范
RSpec.describe 'AppOmniAuthentication', type: :concern do
let(:klass) { User }
let(:user) { create(:user) }
let(:user_oauth_json_response) do
unfiltered_oauth_packet = load_json_fixture('app_omni_authentication_spec')
unfiltered_oauth_packet['provider'] = unfiltered_oauth_packet['provider'].to_sym
unfiltered_oauth_packet['uid'] = unfiltered_oauth_packet['uid'].to_i
unfiltered_oauth_packet
end
before do
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:app] = OmniAuth::AuthHash.new(
user_oauth_json_response,
credentials: { token: ENV['APP_CLIENT_ID'], secret: ENV['APP_CLIENT_SECRET'] }
)
end
describe '#from_omniauth' do
let(:app_oauth) { OmniAuth.config.mock_auth[:app] }
it 'returns varying oauth related data for Bigcartel OAuth response' do
data = klass.from_omniauth(app_oauth)
expect(data[:provider]).to eq(user_oauth_json_response['provider'].to_s)
expect(data[:uid]).to eq(user_oauth_json_response['uid'].to_s)
expect(data[:email]).to eq(user_oauth_json_response['info']['email'])
expect(data[:customer_ids]).to eq(user_oauth_json_response['extra']['raw_info']['customer_ids'])
end
end
describe '#refresh_access_token!' do
it 'false if OAuth2 Fails' do
allow(user).to receive(:access_token) { true }
allow(user).to receive(:refresh_access_token!) { false }
allow(user).to receive(:result).and_raise(OAuth2::Error)
expect(user.refresh_access_token!).to be_falsey
end
it 'false if refresh fails' do
allow(user).to receive(:access_token) { true }
allow(user).to receive(:refresh_access_token!) { false }
expect(user.refresh_token!).to be_falsey
end
it 'true if new token' do
allow(user).to receive(:access_token) { true }
allow(user).to receive(:refresh_access_token!) { true }
expect(user.refresh_token!).to be_truthy
end
it 'true when refreshed' do
auth_token = OpenStruct.new(token: FFaker::Lorem.characters(50),
refresh_token: FFaker::Lorem.characters(50),
expires_at: 5.days.from_now)
allow(user).to receive_message_chain('access_token.refresh!') { auth_token }
expect(user.refresh_access_token!).to be_truthy
end
end
describe '#refresh_token!' do
it 'false if no access token' do
allow(user).to receive(:access_token) { false }
expect(user.refresh_token!).to be_falsey
end
it 'false if refresh fails' do
allow(user).to receive(:access_token) { true }
allow(user).to receive(:refresh_access_token!) { false }
expect(user.refresh_token!).to be_falsey
end
it 'true if new token is saved' do
allow(user).to receive(:access_token) { true }
allow(user).to receive(:refresh_access_token!) { true }
expect(user.refresh_token!).to be_truthy
end
end
describe '#token expired?' do
it 'true if valid' do
expect(user.token_expired?).to be_falsey
end
it 'false if expired' do
user.token_expires_at = 10.days.ago
expect(user.token_expired?).to be_truthy
end
end
end
更新
我将当前规范更改为:
it 'false if OAuth2 Fails' do
allow(OAuth2::AccessToken).to receive(:access_token) { Class.new }
allow(OAuth2::AccessToken).to receive_message_chain('access_token.refresh!') { raise OAuth2::Error.new('ERROR') }
binding.pry
end
但是,现在出现以下错误:
However, now I am getting the following error:
WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request..
推荐答案
看起来关键是这里的第二行:
It looks like the key is the second line here:
def refresh_access_token!
result = access_token.refresh!
store_token(result)
save
rescue OAuth2::Error
false
end
所有其他测试存根access_token
.如果您有一个调用了real方法的测试,它将覆盖access_token
,client
,strategy
和settings
中的缺失行.
All your other tests stub access_token
. If you had a test that called the real method, it would cover the missing lines in access_token
, client
, strategy
, and settings
.
现在strategy
,settings
和client
都很无聊:前两个本质上只是常量.而client
只是通过初始化就不会做任何事情.因此,打电话给那些应该没什么大不了的.剩下的只是access_token
.您会看到它初始化了OAuth2::AccessToken
,其代码为在Github上.初始化程序也很无聊.它只是保存输入以便以后使用.
Now strategy
,settings
, and client
are all pretty boring: the first two are essentially just constants. And client
doesn't do anything just by initializing it. So calling those should be no big deal. That leaves just access_token
. You can see that that initializes an OAuth2::AccessToken
, whose code is on Github. The initializer is also pretty boring. It just saves the inputs to use later.
因此,我将对它的refresh!
方法进行存根:一次返回一个看上去有效的刷新令牌,一次返回一个OAuth2::Error
.您可以使用expect_any_instance_of
来做到这一点.如果这让您感到不安,则还可以对:new
本身进行存根处理,并使其返回自己的假对象:
So I would stub its refresh!
method: once to return a valid-looking refresh token, and once to raise an OAuth2::Error
. You can use expect_any_instance_of
to do that. If that makes you uneasy you could also stub :new
itself, and have it return your own fake object:
o = expect(OAuth2::AccessToken).to receive(:new) { Object.new }
expect(o).to receive(:refresh!) { raise OAuth2::Error.new("something here") }
It looks like it might be a little bit of a nuisance to construct an OAuth2::Error
since it takes a request object, but I don't see anything too complicated there, just messy.
那应该给你100%的覆盖率!
That should give you 100% coverage!
这篇关于Oauth模型关注测试覆盖范围存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!