RhinoMocks - stubbed method only called once - rhino-mocks

Here's some test code:
var searchCommand = MockRepository.GenerateStub<ISearchCommand>();
activityCreatedDateQuery = new ActivityCreatedDateQuery(searchCommand, maxRows);
searchCommand.Stub(x => x.GetResults(activityCreatedDateQuery))
.Return(GetCreatedDateQueryMockData()));
Here's the thing...when I initially call activityCreatedDateQuery.ExecuteQuery() which internally calls searchCommand.GetResults(this), GetCreatedDateQueryMockData() is called as expected.
The second time I call this in the thread, GetCreatedDateQueryMockData() is not called, instead the previous call's result is (resulting in an IDataReader closed exception).
This is obviously behaviour by design, so how do I make sure the delegate is always called...i've explored Stub.Repeat.WhenCalled.CallBack etc with no luck...

The problem is that "Return" doesn't take a delegate, it just takes an object. The GetCreatedDataQueryMockData() method call is executed at the time your Stub is set and the value is saved inside the framework as the return value.
What you need is a true delegate to be called every time the stub is called. Someone else asked about this on Stackoverflow recently and I created a little extension method to do this.

I had a similar problem.
searchCommand.Stub(x => x.GetResults(activityCreatedDateQuery))
.Return(null).WhenCalled(i => i.ReturnValue = GetCreatedDateQueryMockData()));
worked in my case. It works because "WhenCalled()" unlike "Return()" uses delegates.
When using "WhenCalled()" do not forget to put a dummy value in "Return()".

Try
searchCommand.Stub(x => x.GetResults(activityCreatedDateQuery))
.Return(GetCreatedDateQueryMockData())).Repeat.Any();

Related

Mockito - Is it possible to deep mock a void method call to do nothing?

I wanted to know if it is possible to "deep mock" a void method call without breaking out the call chain, using Mockito.
This is an example for the original call I want to mock:
obj.getSomething().add(3);
where "add"'s return type is void.
I tried:
doNothing().when(obj).getSomething().add(3)
and:
doNothing().when(obj.getSomething()).add(3) //wont work since "when" expects a mock.
I also failed using Mockito.when(...) since it does not work with void methods.
I do not want to break the call up since it will be very cumbersome for fluent API calls that are much longer.
Is there an official solution / workaround for this scenario?
Thanks :)
If the value returned by getSomething is not a mock, it won't work.
Return value of getSomething should be a mock and it will allow to assign mock behavior for that value.
Something someMock = mock(Something.class);
when(obj.getSomething()).thenReturn(someMock);
doNothing().when(someMock).add(3);

Calling OCMStub and OCMReject on the same method

I've been attempting to write some fail fast tests using OCMReject. However I've found that if OCMStub is used in conjunction with OCMReject, this test will pass
id _mockModel = OCMProtocolMock( #protocol( CTPrefModelProtocol));
//It doesn't seem to matter what order these two are in, the test behaves the same
OCMStub([_mockModel getPreferences]);
OCMReject([_mockModel getPreferences]);
[_mockModel getPreferences];
Even though it should clearly fail because I'm calling the function that I've set in the OCMReject method.
I realise I can just stub getPreferences whenever I'm expecting a result from it and remove it from this test, but largely that means if I've set a stub on getPreferences in my setUp method, any test that calls OCMReject([_mockModel getPreferences]) will just be ignored.
Why am I not able to use OCMStub and OCMReject together? Is it because OCMStub alters getPreferences somehow and as a result whenever I call this method, it actually calls some other method instead?
So apparently I can't read. Reading through the OCMock 3 Documentation, under the limitations heading 10.2
Setting up expect after stub on the same method does not work
id mock = OCMStrictClassMock([SomeClass class]);
OCMStub([mock someMethod]).andReturn(#"a string");
OCMExpect([mock someMethod]);
/* run code under test */
OCMVerifyAll(mock); // will complain that someMethod has not been called
The code above first sets up a stub for someMethod and afterwards an
expectation for the same method. Due to the way mock objects are
currently implemented any calls to someMethod are handled by the stub.
This means that even if the method is called the verify fails. It is
possible to avoid this problem by adding andReturn to the expect
statement. You can also set up a stub after the expect.
I suspect this same limitation exists for OCMReject as well. Hopefully this helps equally blind people like myself. A link to the documentation for the lazy.

How to use OCMock to verify that an asynchronous method does not get called in Objective C?

I want to verify that a function is not called. The function is executed in an asynchronous block call inside the tested function and therefore OCMReject() does not work.
The way I have tested if async functions are indeed called would be as follows:
id mock = OCMClassMock([SomeClass class]);
OCMExpect([mock methodThatShouoldExecute]);
OCMVerifyAllWithDelay(mock, 1);
How would a test be done to test if a forbidden function is not called?
Something like:
VerifyNotCalled([mock methodThatShouoldExecute]);
OCMVerifyAllWithDelay(mock, 1);
I would recommend using an OCMStrictClassMock instead of the OCMClassMock (which gives you a nice mock). A strict mock will instantly fail your test if any method is called on it that you did not stub or expect, which makes your tests a lot more rigorous.
If that's not an option for you, you can do what you described with:
OCMReject([mock methodThatShouoldExecute]);
See the "Failing fast for regular (nice) mocks" section in the OCMock docs.
Now as for waiting for your code which may call the forbidden method, that's another matter. You can't use OCMVerifyAllWithDelay since that returns immediately as soon as all expectations are met, it doesn't wait around a full second to see if illegal calls will be made to it. One option is to put a 1 second wait before verifying the mock each time. Ideally, you could also wait explicitly on your asynchronous task with an XCTestExpectation. Something like:
XCTestExpectation *asyncTaskCompleted = [self expectationWithDescription:#"asyncTask"];
// Enqueued, in an onCompletion block, or whatever call
// ... [asyncTaskCompleted fulfill]
[self waitForExpectationsWithTimeout:1 handler:nil]

OCMock and overriding stub value

mockModule = OCMPartialMock(module);
OCMStub([mockModule send:#"FOO"]).andReturn(YES);
OCMStub([mockModule send:#"FOO"]).andReturn(NO);
In this example I have a simple mock module, and I set some stubs to return YES/NO when sent a String, the problem that occurs is that if I set the same string twice it only returns the first value, and not the new value.
In this example about the problem is demonstrated like so I would expect a call such as:
BOOL answer = [module send:#"FOO"]
//answer should be NO, but is YES
How can I make it respond with the most recently set value?
You could use the expect methods, e.g.
mockModule = OCMPartialMock(module);
OCMExpect([mockModule send:#"FOO"]).andReturn(YES);
OCMStub([mockModule send:#"FOO"]).andReturn(NO);
That's not exactly what they are meant for, but it does make some sense. You're basically saying, I expect that send: will be called, and when that has actually happened, then I want the method to be stubbed.
Also, if it were possible to "pile up" the stubs, figuring out what went wrong would be quite difficult, e.g. if the first invocation of the stub doesn't happen, then the second invocation will get the value meant for the first.

Rhino Mocks WhenCalled - Delegate 'System.Action' does not take 1 arguments

WhenCalled is well documented all over the place & I'm not going into why I'm using it but I just can't get it to work in the same way as it's detailed inpractically every post I've seen on the topic.
Basically you should be able to do something like:
mock.Expect(x => x. SingleOrDefault(null))
.IgnoreArguments()
.WhenCalled(invocation => ((Action)invocation.Arguments[0]).Invoke());
But this doesn't compile for me - I just get an error saying Delegate 'System.Action' does not take 1 arguments.
I'm using V 4.0 of Rhino Mocks - has this method changed? I want to use
WhenCalled to grab the arguments passed to SingleOrDefault (in this case a lambda expression).
All answered in this blog post:
Rhino Mocks 4.0.0
Previously, WhenCalled allowed you to execute an Action that took, as a single parameter, a data structure encapsulating the method invocation. Since an Action was being invoked the return value of the expectation could not be changed.
In the current version, WhenCalled has been modified slightly to simply execute an Action
In order to provide a similar feature as the original “WhenCalled”, a new method “Intercept” has been added which expects an Action that takes, as a single parameter, a data structure encapsulating the method invocation
tl;dr: Use Intercept instead of WhenCalled