0

I am doing OCMocktest for methods that will return some value. How to implement for void methods. Eg a method that calculates Simple interest. But it doesot return any value. How can check the calculation functionality?

-(void)calculateSI
{
float Result= PXnXr/100;
}

How to check whether Result=100 using OCMock? Can I implement only for methods that has return type?

  1. How to test whether calculateSI method is called or not?

  2. How to test whether Result=100?

halfer
  • 19,824
  • 17
  • 99
  • 186
Warrior
  • 39,156
  • 44
  • 139
  • 214

2 Answers2

0

1) If you want to test whether calculateSI was called or not you should mock that object. Something similar to this:

id calculator = [OCMockObject mockForClass:[SICalculator class]];
systemUnderTest.calculator = calculator

[[calculator expect] calculateSI];

[systemUnderTest calculateSomething];

[calculator verify];

2) You can't test a local variable value. You should make result a property or be returned.

juanignaciosl
  • 3,435
  • 2
  • 28
  • 28
  • "System Under Test" (SUT) is a common name for the class you're actually testing. You said you wanted to test whether that method was invoked or not, so you want to test the interaction between somebody else (SUT) and its dependency (the calculator). For example, you might want to test that tapping a button in a view invokes the calculator. In that case the view is the SUT. – juanignaciosl May 08 '13 at 07:39
0

Instead you can modify your method to take the principle amount as argument and return the calculated SI as show below.

- (float)calculateSI:(float)inPXnXr
{
     return inPXnXr/100;
}

and your test case will become

- (void)test__calculateSI__invokeWith300
{
     CalculatorViewController *masterVC = [[CalculatorViewController alloc] init];
     float result = [masterVC calculateSI:300.0];
     STAssertTrue(result == 3, @"FAILED");
}

You dont need to use OCMock for this because you are testing the result of a method. You can directly call the method and test the return value.

Prasad Devadiga
  • 2,573
  • 20
  • 43