2016-06-16 5 views
1

私はRSpecを学んでおり、チュートリアル以外のコードでは悲惨なことに失敗しているようです。だからこそ私はこの問題で頭を壁に叩いている。うまくいけば皆が助けることができる。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の学生から理解されます。 乾杯!

答えて

1

describe RSpecのブロックは、Rubyの名前空間に影響しません。あなたの仕様では、NegotiationNegotiGate::Negotiationとして参照する必要があります。しかし

、あなたが本当にそのスペックに記述していることはNegotiGateではなくNegotiGate::Negotiation、そうdescribe NegotiGate::Negotiationdescribeブロックを変更して、あなたが短いためdescribed_classを使用することができます。

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 

ちなみに、チェックアウトRSpecのletは、複数のテストで使用される変数を定義する現代的な方法です。実際にあなたが示していることでは、あなたの例でローカルを宣言すべきですが、おそらくより多くのテストを書くことになり、negotiateを一度定義する価値があります。ああ、negotiationという名前を付けてクラス名と一致させると、negotiateより良いでしょう。

+0

ありがとうございます!私はちょうど始まっています、そして、時々周りを頭で包み込むためのテストは難しいです。 –

0

代わりにNegotiGate::Negotiation.newを試してください。

関連する問題