首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为这段代码编写RSpec测试代码?

如何为这段代码编写RSpec测试代码?
EN

Stack Overflow用户
提问于 2016-12-22 18:29:59
回答 2查看 83关注 0票数 0

我正在学习RSpec测试。现在我想写关于下面的代码的rspec,但我不能。

代码语言:javascript
复制
module FrontendHelper
  def scholarship_style_change(scholarship)
    array = []
    if scholarship.discount >= 50 && scholarship.id.odd?
      array = ['OliveDrab', 'box box-border']
    elsif scholarship.discount >= 25 && scholarship.id.even?
      array = ['FireBush', 'box box-theme']
    else
      array = ['Mojo', 'box box-dark']
    end
    array
  end
end

有人能帮我写这个方法的rspec测试代码吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-22 19:49:49

以下是该方法的测试代码:

代码语言:javascript
复制
RSpec.describe FrontendHelper, type: :helper do
  describe '#scholarship_style_change(scholarship)' do
    let!(:scholarship_group) { FactoryGirl.create :admin_scholarship_group }
    let(:scholarship_50) { FactoryGirl.create :admin_scholarship, name: '50% off', discount: 50, condition: 'GPA 5', details: 'only GPA 5', group: scholarship_group  }
    let!(:scholarship_25) { FactoryGirl.create :admin_scholarship, name: '25% off', discount: 25, condition: 'GPA 4.25', details: 'only GPA 4.25', group: scholarship_group }
    let!(:scholarship_20) { FactoryGirl.create :admin_scholarship, name: '20% off', discount: 20, condition: 'GPA 4', details: 'only GPA 4', group: scholarship_group  }
    let(:first_array) { ['OliveDrab', 'box box-border'] }
    let(:second_array) { ['FireBush', 'box box-theme'] }
    let(:third_array) { ['Mojo', 'box box-dark'] }

    context 'checks above 50% scholarship' do
      it 'returns first array' do
      expect(scholarship_style_change(scholarship_50)).to eq(first_array)
      end
      it 'does not return second array' do
        expect(scholarship_style_change(scholarship_50)).not_to eq(second_array)
      end
      it 'does not return third array' do
        expect(scholarship_style_change(scholarship_50)).not_to eq(third_array)
      end
    end

    context 'checks above 25% scholarship with even id' do
      it 'does not return first array' do
        expect(scholarship_style_change(scholarship_25)).not_to eq(first_array)
      end
      it 'does not return second array' do
        expect(scholarship_style_change(scholarship_25)).not_to eq(second_array)
      end
      it 'returns third array' do
        expect(scholarship_style_change(scholarship_25)).to eq(third_array)
      end
    end

    context 'checks above 20% scholarship with odd id' do
      it 'does not return first array' do
        expect(scholarship_style_change(scholarship_20)).not_to eq(first_array)
      end
      it 'does not return second array' do
        expect(scholarship_style_change(scholarship_20)).to eq(second_array)
      end
      it 'does_not return third array' do
        expect(scholarship_style_change(scholarship_20)).not_to eq(third_array)
      end
    end
  end
end
票数 0
EN

Stack Overflow用户

发布于 2016-12-22 18:49:54

创建一个虚拟类来扩展模块并通过类实例测试方法

代码语言:javascript
复制
before(:each) do
  @helper_class = Class.new { extend FrontEndHelper}
  @helper_class.extend(FrontEndHelper)
end

it "returns OliveDrab and box-border if discount above 50 and with odd id" do
   scholarship = double("scholarship", discount: 55, id: 7)
   @helper_class.scholarship_style_change(scholarship).should eq(['OliveDrab', 'box box-border'])
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41289611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档