Overwrite object instance with object from mock.Expect () call

I have a method that I am testing.

void MethodIAmTesting()
{
   SomeObject so = new SomeObject();

   _someService.AnotherMethodCall(so);
}

Now I'm trying to make fun of “like that” in my Unit Test as such.

void UnitTestMethod
{
   SomeObject testSO = new SomeObject();
   //Fill in data here.

   _classMock.Expect(instance => instance.AnotherMethodCall(testSO));

   _mainClass.MethodIAmTesting();
}

But when I run the test, it just uses the empty version created in the real method. How can I get the method I'm calling to use my testSO instead from the real code? Or is it impossible?

Thank.

+3
source share
1 answer

The test-based design part should stimulate you depending on the injection. Often, when you find something difficult for unit testing, this is because you need to rethink your design.

-, , SomeObject. -. , , , , , , , , . :

public class ClassBeingTested
{    
  public ClassBeingTested(SomeObject so)
  {
    // Inject class dependency when instantiated the class
  }

  void MethodIAmTesting(SomeObject so)
  {
    // Passing things needed for a function to complete its work
  }   
}

, , , SomeObject. , , / .. , . , , , _someService SomeObject:

void EnsureSomeObjectPassedToService()
{
   // arrange
   SomeObject so = new SomeObject()
   // act
   _mainClass.MethodIAmTesting(so);
   // assert
   _someObject.AssertWasCalled(x => x.AnotherMethodCall(so));
}

! , .

0

All Articles