2016-12-05 7 views
-1

저는 rspec 테스트를 처음 접했고 사용자 위치를 테스트하는 데 문제가 있습니다. 이것은 특정 지역의 스팸을 차단하기 위해 country_code의 행동을 조롱하는 테스트입니다. 수변수를 rspec 테스트에 전달

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 '

: 오류 코드 여기

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 

을 그리고있다 : 여기

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 

내 사양 파일의 코드는 다음과 같습니다

내 서비스의 코드 아무도 도와주지?

답변

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