OCMock report error from stub - objective-c

I want to test that my code properly handles a particular error returned from dataWithContentsOfFile. The problem is that the error isn't specifically returned from the method, it is an output parameter (passed in as NSError **.
It is easy enough for me to simply have the mock return nil from the stub, but I want to specifically test that the error it outputs is handled. How can I achieve that?

I think what you're looking for is described in section 2.5 (Returning values in pass-by-reference arguments) in the documentation: http://ocmock.org/reference/#stubing-methods

Related

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.

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.

Call functions from smart contract

could I interact with functions in my deployed contract without using truffle-contract?
I just want to run and play with my functions to check how they work.
I used MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."});
but it doesn't work.
Any idea or suggestions
Thanks
It's hard to know what you mean by "it's doesn't work", but I guess is that you are not seeing any output when running MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}); in the truffle console?
If yes, the reason is because invoking a method to an instance of a contract will give you a Future, and you must handle the result coming back from the call in an asynchronous way. For example, if the function return a value indicating that some computation has happened, you can print the returned value in the console by:
MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}).then(console.log)
If you're writing unit tests (to be executed via truffle test), then you can handle the return value by doing this:
MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}).then(function(returnedValue) {
// do something with the returnedValue, e.g.
// assert.equal(returnedValue, 3, "The returned value must be 3");
});

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

iOS - Method With Success And Error Callbacks Without Initial Parameter

I use success and error callback blocks a lot in method definitions with initial parameters like so:
+(void)doSomethingWithObject:(MyObject*)myObject successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;
where myObject is the initial parameter. However, I have come across a situation right now where I don't need any parameters. I'm trying to define my method like so:
+(void)getSomeData successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;
But now Xcode is giving me some syntax complaints. How can I define a method without any initial parameters but also having a success and error callback? Is this impossible or is there just something I don't understand about the correct syntax?
You should edit to
+(void)getSomeDataSuccessCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;