【问题标题】:Passing variable to an rspec test将变量传递给 rspec 测试
【发布时间】:2017-04-20 00:55:14
【问题描述】:

我对 rspec 测试还很陌生,但在尝试测试用户的位置时遇到了问题。这是一个模拟 country_code 行为以阻止来自特定区域的垃圾邮件的测试。

这是我的服务代码:

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

这是我的规范文件的代码:

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

这里是失败代码:

失败:

1) GeocodeUserAuthorizer,用户从授权连接 国家 失败/错误:它{期望(GeocodeUserAuthorizer.new.authorize!(user_country_code:{“CA” })).to eql(true) } 参数错误: 缺少关键字:user_country_code # ./app/services/geocode_user_authorizer.rb:2:in initialize' # ./spec/services/geocode_user_authorizer_spec.rb:16:innew' # ./spec/services/geocode_user_authorizer_spec.rb:16:in block (3 levels) in <top (required)>' # ./spec/spec_helper.rb:56:inblock (3 个级别) in ' # ./spec/spec_helper.rb:56:in `block (2 levels) in '

有人可以帮忙吗?

【问题讨论】:

    标签: ruby-on-rails ruby rspec rspec-rails rspec2


    【解决方案1】:

    你没有正确调用你的类,你的构造函数需要国家代码。试试这个:

    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,您还需要在课程中为 user_country_code 添加一个 attr_reader。

    【讨论】:

      【解决方案2】:

      好的,所以我的测试太复杂并且缺少分隔符。这是一个有效的最终版本。

      测试:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 2021-01-24
        • 1970-01-01
        • 1970-01-01
        • 2015-03-10
        • 2021-02-18
        相关资源
        最近更新 更多