2016-07-25 59 views
0

在我的訂單模型中,我包含一個PORO類「ShipmentHandler」。這PORO位於這樣的:應用程序/模型/順序/ shipment_handler.rbRspec測試包含在模型導軌中的PORO

我祈求這在我的訂單模式,像這樣:

def assign_shipments 
    ShipmentHandler.new(self).assign_shipments 
end 

我PORO樣子:

class Order 
    class ShipmentHandler 
     def initialize(order) 
      @set_some_variables 
     end 

     def some_methods 
     end 
    end 
end 

我試圖創建spec來測試ShipmentHandler類中的方法。我不知道如何做到這一點,因爲我喜歡保持uninitialized constant ShipmentHandler

收到錯誤我試圖將它添加到我的order_spec.rb像這樣:

describe Order do 
    describe Order::ShipmentHandler do 
    end 
end 

和:

describe Order do 
    describe ShipmentHandler do 
    end 
end 

既不工作。我也嘗試在spec/models/order/shipment_handler_spec.rb中創建一個規範 這也失敗了。

回答

2

寫作規範的下列方式爲我工作,當我對你們的Order類的樣子與嵌套ShipmentHandler類中的一些假設:

class Order 
    def assign_shipments 
    ShipmentHandler.new(self).assign_shipments 
    end 

    class ShipmentHandler 
    def initialize(order) 
     @order = order 
    end 

    def some_methods 
    end 
    end 
end 

RSpec.describe Order do 
    it { is_expected.to be_a Order } 
end 

# Method 1 
RSpec.describe Order::ShipmentHandler do 
    subject(:shipment_handler) { described_class.new(Order.new) } 

    it { is_expected.to be_a Order::ShipmentHandler } 
end 

# Method 2 
class Order 
    RSpec.describe ShipmentHandler do 
    subject(:shipment_handler) { described_class.new(Order.new) } 

    it { is_expected.to be_a Order::ShipmentHandler } 
    end 
end 
+0

你,先生,是我的英雄。非常感謝! – stoerebink