I'm trying to verify that a function on a mock object is NOT called at all with ANY parameters.
The function on the object I'm mocking is...
- (void)registerUserWithUsername:(NSString*)username password:(NSString*)password;
I'd like to verify that the function is NOT called if the username is blank.
i.e.
[mockService registerUserWithUsername:#"" password:#"Password"];
[verify(mockService, never()) registerWithUsername:....... password:.......];
I'm just not sure what to put in the ...... bits?
To specify that it's never called with any parameters, use the anything() matcher:
[verify(mockService, never()) registerWithUsername:(id)anything() password:(id)anything()];
Related
I have this method:
Future<Either<Failure, WorkEntity>> updateWorkEntity({int id, String title, TimeType timeType, int times, DateTime executed})
that is being called like this:
repository.updateWorkEntity(id: workEntity.id, executed: DateTime.now())
the id I can control in a test, but the "DateTime.now()" I ofcourse can not. What I tried was this in my test:
when(repository.updateWorkEntity(id: expected.id, executed: any)).thenAnswer((_) async => Right(expected));
to be able to make my mock return a object for my test, by using "any" in the place of the "DateTime.now()", but I get this error:
Invalid argument(s): The "any" argument matcher is used outside of
method stubbing (via when) or verification (via verify or
untilCalled). This is invalid, and results in bad behavior during
the next stubbing or verification.
So I guess I can not use any here, but then how do I get my mock to return an object when I do not control one of the input parameters?
Thank you
Søren
Use executed: anyNamed('executed') instead of executed: any
In the integration test, i am using groovy mock feature to mock a service method like shown below:
def mock = new groovy.mock.interceptor.MockFor(PaymentService)
mock.demand.processPayment(){ a, b, c-> ['status': true, 'approved': true, 'tresponse': new TransactionResponse(amount: total.toBigDecimal(), transactionId: "4234234555", saleId: Sale.last().id, responseCode: "1", responseReasonCode: "1", responseReasonText: "approved", authorizationCode: "asdasd", paymentMethod: "CC", transactionType: "auth_capture", cardCodeResponse: "P").save(flush: true)]}
mock.use{
controller.paymentService = new PaymentService()
populateReceiptParams(total)
controller.receipt()
}
The payment controller method receipt() uses payment service method processPayment which communicates with authorize.net. So i have mocked out this method as shown above.
When the test is run, the error i am getting is as follows
junit.framework.AssertionFailedError: No call to 'getMergedSale' expected at this point. Still 1 call(s) to 'processPayment' expected.
at PaymentController.cart(PaymentController.groovy:296)
at PaymentController.receipt(PaymentController.groovy:1096)
So the thing is that inside receipt method there is another call to paymentservice being made
paymentService.getMergedSale([sessionAuth, userAuth])
So does this mean that the mocked method which is processPayment must be called first before getMergedSale? I appreciate any guide as to the reason for this error. Thanks!
So does this mean that the mocked method which is processPayment must
be called first before getMergedSale?
Not necessarily, no. It does mean that you need to provide a mocked implementation of getMergedSale.
Moonscript uses \ to call methods so can someone explain to me why the code below does not work:
> file = io\open("mix.exs", "rb")
[string "tmp"]:1: calling 'open' on bad self (string expected, got table)
but when you call it to read the file it does ?
> file\read!
"Code.ensure_loaded?(Hex) and Hex.start
The io.open function expects to get a string as the first argument but io\open (like io:open in lua itself) is actually passing the io table as the first argument. That is it is a method call.
io\open("file", "mode")/io:open("file", "mode") are syntactic sugar for io.open(io, "file", "mode").
This is why file\read! works without an explicit argument because file gets passed as the first argument to the read("file", "format") function.
Moonscript uses \ to call methods
to call member methods. as in a\b c, ... translates to a.b(a,c,...).
this doesn't work here because io.open is a static function (io.open(what,how)), not a member (io.open(self,what,how)).
you couldn't call io:open in Lua either. the only place where io functions allow for being called as members is when you want to read/write stdio.
but when you call it to read the file it does ?
because now it's a member method of the file. you're actually still using io.read there, but the file object has io as a metatable index, therefore allowing you to access the same function via file.read, and since file\read! translates to file.read(file) it's the same thing.
so essentially the answer boils down to "because io:open doesn't work in Lua".
How can I verify that my mock object created with OCMockito has received a method invocation with a primitive argument?
The method that I'm trying to test is setProgress:(float)progress
CompositeProgressView* mockProgress = mock([CompositeProgressView class]);
self.downloader.progressView = mockProgress;
//run a task that increments progress
...
//test
[verify(mockProgress) setProgress:anything()]; //does not work
[[verify(mockProgress) withMatcher:anything()] setProgress:0];
A described in "How do you specify matchers for primitive arguments?" at https://github.com/jonreid/OCMockito.
Hope that helps!
I don't think this is possible, but is there a way to validate the arguments a callback accepts. For example someone passes me callback "mycallback", I want to assert it accepts an argument String.
public function addHandler(handler : Function) : void{
//pseudo code
Assert.functionAcceptsArguments(handler, String);
}
Cheers
No you can't. If you had some object as argument you could use describeType which returns information about methods too. But Function is just a function with no information about arguments.
To be sure that you get right method signature you could pass typed objects not Functions. For example
interface Bla {
funciton invoke(value:String):void;
}
function addHandler(handler:Bla):void {}