Ruby Can';RSpec中模块内的t测试类

Ruby Can';RSpec中模块内的t测试类,ruby,oop,rspec,tdd,bdd,Ruby,Oop,Rspec,Tdd,Bdd,我正在学习RSpec,似乎在教程之外的任何代码中都失败得很惨。这就是为什么我的头撞在墙上这个问题。希望你们都能帮忙 自由/谈判门 module NegotiGate class Negotiation def initialize(price1, price2) @price1 = price1 @price2 = price2 end def both_prices_equal?(price1, price2) if @pri

我正在学习RSpec,似乎在教程之外的任何代码中都失败得很惨。这就是为什么我的头撞在墙上这个问题。希望你们都能帮忙

自由/谈判门

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
规范/谈判门\规范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
NegotiGate
谈判
.两种价格相等吗?
鉴于卖方的价格与买方的最高购买价格相同
返回true(失败-1)
失败:
1) 谈判双方价格相等吗?如果卖方的价格与买方的价格相同,则最高购买价格返回真实值
失败/错误:@negotiate=Negotiation.new(10,10)
名称错误:
未初始化常数协商
#./spec/NegotiGate_spec.rb:6:in‘块(2层)in’
在0.00134秒内完成(加载文件需要0.15852秒)
1例,1例失败
失败的示例:
rspec./spec/NegotiGate_spec.rb:11#NegotiGate NegotiGate NegotiGate NegotiGate NegotiGate.两个价格相等吗?如果卖方的价格与买方的价格相同,则最高购买价格返回真实值
TDD的学生非常感谢任何帮助。
干杯

尝试
NegotiGate::negotigation。改用新的

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

但是,您在该规范中真正描述的不是
NegotiGate
,而是
NegotiGate::Negotiation
,因此将
descripe
块更改为
descripe NegotiGate::Negotiation
,然后您可以使用
descriped\u 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
,这是定义多个测试中使用的变量的现代方法。实际上,在您展示的示例中,您应该只在您的示例中声明一个local,但您可能会编写更多的测试,这样就值得定义一次。哦,把它命名为
协商
来匹配类名要比
协商
好,谢谢!我才刚刚开始,有时测试对我来说很难让我的头脑清醒。