2016-12-05 7 views
-1

私はrspecテストをかなり新しくしており、ユーザーの場所をテストしようとする際に問題があります。これは特定のゾーンからのスパムをブロックするcountry_codeの動作を模擬するテストです。ここで変数をrspecテストに渡す

は私のサービスのコードです:ここで

class GeocodeUserAuthorizer 
    def initialize(user_country_code:) 
    @user_country_code = user_country_code 
    end 

    def authorize! 
    user_continent = ISO3166::Country.new(user_country_code).continent 

    if user_continent == 'Africa' 
     return true 
    else 
     return false 
    end 
    end 
end 

は私のspecファイルのコードです:

require 'spec_helper' 

describe GeocodeUserAuthorizer do 
    context 'with a user connecting from an authorized country' do 
    it { expect(GeocodeUserAuthorizer.new.authorize!(user_country_code: { "CA" })).to eql(true) } 
    end 
end 

そしてここでは、故障コードは次のとおりです。

Failures:

1) GeocodeUserAuthorizer with a user connecting from an authorized country Failure/Error: it { expect(GeocodeUserAuthorizer.new.authorize!(user_country_code: { "CA" })).to eql(true) } ArgumentError: missing keyword: user_country_code # ./app/services/geocode_user_authorizer.rb:2:in initialize' # ./spec/services/geocode_user_authorizer_spec.rb:16:in new' # ./spec/services/geocode_user_authorizer_spec.rb:16:in block (3 levels) in <top (required)>' # ./spec/spec_helper.rb:56:in block (3 levels) in ' # ./spec/spec_helper.rb:56:in `block (2 levels) in '

ことができます誰か助けて?

答えて

0

クラスを正しく呼び出さなかったため、コンストラクターに国コードが必要です。これを試してみてください:

describe GeocodeUserAuthorizer do 
    context 'with a user connecting from an authorized country' do 
    it { expect(GeocodeUserAuthorizer.new(user_country_code: { "CA" })).authorize!).to eql(true) } 
    end 
end 

また、あなたはauthorize!@シンボルなしでそれを使用したい場合は、あなたのクラスにuser_country_codeためattr_readerを追加したいと思うでしょう。

0

私のテストは複雑すぎて、分離が不十分でした。ここには動作する最終版があります。

テスト:

require 'spec_helper' 

describe GeocodeUserAuthorizer do 
    let(:geocode_authorizer) { GeocodeUserAuthorizer.new(country_code: country_code) } 

    context 'with a user connecting from an unauthorized country' do 
    let!(:country_code) { 'AO' } 

    it { expect(geocode_authorizer.authorize!).to eql(false) } 
    end 

    context 'with a user connecting from an authorized country' do 
    let!(:country_code) { 'CA' } 

    it { expect(geocode_authorizer.authorize!).to eql(true) } 
    end 
end 

サービス:

class GeocodeUserAuthorizer 
    def initialize(country_code:) 
    @country_code = country_code 
    end 

    def authorize! 
    check_country 
    end 

    protected 

    def check_country 
     user_continent = ISO3166::Country.new(@country_code).continent 

     if user_continent == 'Africa' 
     return false 
     else 
     return true 
     end 
    end 
end