0

このようなクラスのRpecでテストケースを書き込もうとしています

class ABC
   def method_1( arg )
       #does something and return a value based on arg
   end

   def initialize
       @service = method_1( arg )
   end

   def getSomething_1
       return @service.get_seomthing_1
   end

   def getSomething_2
       return @service.get_seomthing_2
   end
end

ここで、モック オブジェクトを使用して @service インスタンス変数を開始し、そのモック オブジェクトを使用して、単体テストを検証できる値を返すことができるようにします。

私は次のようなことをしようとしました

describe ABC do
    before(:each) do
       myObject = double()
       myObject.stub(:get_something_1).and_return("SomeValue")
       ABC.any_instance.stub(:method_1).and_return(myObject)
    end

   it "Checks the correctness of getSomething_1 method of class ABC" do
       @abc = ABC.new
       @abc.getSomething_1.should eql("SomeValue")
   end
end

テストを実行しようとすると、@service が必要なオブジェクトで初期化されません。method_1 は、定義された動作で嘲笑されていないようです。@service をモック オブジェクトに割り当てる方法を教えてください。

4

2 に答える 2