无法在RSpec

时间:2016-06-16 01:58:31

标签: ruby oop rspec tdd bdd

我正在学习RSpec,似乎在教程之外的任何代码都失败了。这就是为什么我在这个问题上撞到墙上的原因。希望你们都能提供帮助。

LIB / NegotiGate

module NegotiGate
  class Negotiation

    def initialize(price1, price2)
      @price1 = price1
      @price2 = price2
    end

    def both_prices_equal?(price1, price2)
      if @price1 == @price2
        true
      else
        false
      end
    end

  end
end

规格/ NegotiGate_spec.rb

describe NegotiGate do

  before(:each) do
    @negotiate = Negotiation.new(10,10)
  end

  describe "Negotiation" do
    describe ".both_prices_equal?" do
      context "given the sellers price is the same as the buyers highest buying price" do
        it 'returns true' do
          expect(@negotiate.both_prices_equal?).to be_true
        end
      end
    end
  end
end

输出:

NegotiGate
  Negotiation
    .both_prices_equal?
      given the sellers price is the same as the buyers highest buying price
        returns true (FAILED - 1)

Failures:

  1) NegotiGate Negotiation .both_prices_equal? given the sellers price is the same as the buyers highest buying price returns true
     Failure/Error: @negotiate = Negotiation.new(10,10)

     NameError:
       uninitialized constant Negotiation
     # ./spec/NegotiGate_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00134 seconds (files took 0.15852 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/NegotiGate_spec.rb:11 # NegotiGate Negotiation .both_prices_equal? given the sellers price is the same as the buyers highest buying price returns true

TDD的学生非常感谢任何帮助。 干杯!

2 个答案:

答案 0 :(得分:1)

RSpec中的

describe块不会影响Ruby命名空间。在您的规范中,您需要将Negotiation称为NegotiGate::Negotiation无处不在。

但是,您在该规范中真正描述的内容不是NegotiGate而是NegotiGate::Negotiation,因此将describe块更改为describe NegotiGate::Negotiation然后您可以使用{{ 1}}简称:

described_class

顺便说一下,查看RSpec的describe NegotiGate::Negotiation do before(:each) do @negotiate = described_class.new(10,10) end describe ".both_prices_equal?" do context "given the sellers price is the same as the buyers highest buying price" do it 'returns true' do expect(@negotiate.both_prices_equal?).to be_true end end end end ,这是定义多个测试中使用的变量的现代方法。实际上,在你展示的内容中,你应该在你的例子中声明一个本地,但你可能会编写更多的测试,然后值得定义let一次。哦,并命名为negotiate以匹配类名称将比negotiation更好。

答案 1 :(得分:0)

请尝试NegotiGate::Negotiation.new