2016-12-05 5 views
-1

Ich bin ziemlich neu zu RSPC-Test und ich habe ein Problem versucht, den Standort eines Benutzers zu testen. Dies ist ein Test, um das Verhalten eines country_code zu verspotten, um Spam aus bestimmten Zonen zu blockieren. HierÜbergabe der Variablen an einen rspec-Test

ist der Code meines Dienstes:

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 

Hier ist der Code meiner spec-Datei:

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 

Und hier wird der Fehlercode:

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 '

Can jemand Hilfe?

Antwort

0

Sie haben Ihre Klasse nicht korrekt aufgerufen, Ihr Konstruktor benötigt den Ländercode. Versuchen Sie folgendes:

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 

Auch wollen Sie eine attr_reader für user_country_code in Ihrer Klasse hinzufügen, wenn Sie authorize! wollen, es zu benutzen, ohne das @ Symbol.

0

Ok, also war mein Test zu kompliziert und fehlte Separationen. Hier ist eine endgültige Version, die funktioniert.

Der Test:

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 

Der Service:

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 
Verwandte Themen