I'm trying to use qx.dev.unit.MMock.verify() in an asynchronous situation effectively the same as this:
, test_no_output_from_verify: function () {
var mocker = this.mock({ callMeOnce: function () { } });
mocker.expects('callMeOnce').once();
qx.event.Timer.once(function () {
mocker.verify();
this.resume();
}, this, 100);
this.wait(1000);
}
I find that, when the verify() call throws an exception (because the callMeOnce function was not called), that exception is swallowed by the Test Runner. It carries on waiting for the 1000ms timeout to elapse, and then throws the standard timeout exception: 'Asynchronous Test Error: Timeout reached before resume() was called.'.
Is there any way that I can make Test Runner halt immediately and display the exception thrown from mocker.verify() instead?
==============================
EDIT: Reading the manual one more time after posting the question shows my mistake. The correct technique is to put the verify() call inside the resume(), thus:
, test_no_output_from_verify: function () {
var mocker = this.mock({ callMeOnce: function () { } });
mocker.expects('callMeOnce').once();
qx.event.Timer.once(function () {
this.resume(function () {
mocker.verify();
});
}, this, 100);
this.wait(1000);
}
This works as expected and I get the ExpectationError: Expected callMeOnce([...]) once (never called) message inside the Test Runner.
Related
I want to mock requests with ktor's http client using MockK. The problem is all the methods related to making requests with the client are inline, so I cannot use coEvery on those methods. The next thing I tried was to go through the called methods until I found a method that wasn't inline and then mock that. After stepping through some functions, the HttpClient.request() function instantiates an HttpStatement and then calls execute() on it.
public suspend inline fun HttpClient.request(
builder: HttpRequestBuilder = HttpRequestBuilder()
): HttpResponse = HttpStatement(builder, this).execute()
If I can mock the constructor and .execute() functions, I can intercept the call and return my canned response. I can then check that the builder's params are correct inside of a verify function.
mockkConstructor(HttpStatement::class)
coEvery { anyConstructed<HttpStatement>().execute() } returns mockk {
coEvery { status } returns HttpStatusCode.OK
coEvery { body<RefreshToken>() } returns RefreshToken()
}
This code takes care of intercepting the execute call. The next step would be to verify the constructor params of HttpStatement. This code to verify execute was called works:
coVerify { anyConstructed<HttpStatement>().execute() }
Next thing is to verify the constructor params. This pull request in the MockK repo describes how to verify constructors:
coVerify { constructedWith<HttpStatement>(/* Matchers here */).execute() }
Note that I have to add the .execute() or else MockK tells me I'm not verifying anything.
Missing calls inside verify { ... } block.
io.mockk.MockKException: Missing calls inside verify { ... } block.
at app//io.mockk.impl.recording.states.VerifyingState.checkMissingCalls(VerifyingState.kt:52)
at app//io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:21)
...
Ok, so just add in the matchers. However, no combination of matchers I try works. I've tried doing a bunch of constant matchers for type Any (which should match anything right?)
coVerify { constructedWith<HttpStatement>(ConstantMatcher<Any>(true))}
I've tried a matcher for HttpRequestBuilder and HttpClient
coVerify {
constructedWith<HttpStatement>(
ConstantMatcher<HttpRequestBuilder>(true),
ConstantMatcher<HttpClient>(true)
).execute()
}
And a whole slew of others. Each time, I get this error:
Verification failed: call 1 of 1: HttpStatement(mockkConstructor<HttpStatement>(any(), any())).execute(any())) was not called
java.lang.AssertionError: Verification failed: call 1 of 1: HttpStatement(mockkConstructor<HttpStatement>(any(), any())).execute(any())) was not called
at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:63)
at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
...
Next thing I figured I could try would be to use an answers block earlier on in order to print out the types of the parameters being passed in case I was wrong, but that also runs into the "nothing being done in every block" error.
coEvery { anyConstructed<HttpStatement>() } answers {
args.filterNotNull().map { it::class.qualifiedName }.forEach(::println)
mockk {
coEvery { execute().status } returns HttpStatusCode.OK
coEvery { execute().body<RefreshToken>() } returns RefreshToken(
accessToken = accessToken,
expiresIn = expiresIn,
)
}
}
Is there a solution to mocking the http client? Do I have to mock something even more internal? Or do I just have to stick to using the ktor MockEngine?
I am specifically interested in how this nodeJS wrapper for quickfix node-quickfix handles failures on sending FIX messages out. The function signature in question seems to be:
function send([Object], callback fn)
This can be seen in the example given here.
Under the hood, node-quickfix takes advantage of NodeJS nan to bind to C++ code. The underlying quickfix package is C++. What I'm curious about is how an error in this send function, say from a network disconnect, is propagated to JS code. In node-quickfix's FIXSession class it overwrites nan's HandleOKCallback (but not its HandleErrorCallback):
void HandleOKCallback () {
Nan::HandleScope scope;
v8::Local<v8::Function> fn = callback->GetFunction();
if(!(fn->IsUndefined() || fn->IsNull())) {
Local<Value> argv[] = { Nan::Null() };
callback->Call(1, argv);
}
};
The definition of nan's default HandleOKCallback and HandleErrorCallback are here. It seems like the FIXInitiator does have an error callback, but I do not know how it is propagated or accessed in javascript/NodeJS for that matter.
EDIT1: I've added a github issue with what I suspect is the issue but I do not know if it is correct, or what the solution is. There is some additional information with my analysis in the github issue.
EDIT2: Added suggested changes to node-quickfix: FixSendWorker.cpp Execute():
void FixSendWorker::Execute () {
try {
FIX::Session::sendToTarget(*message);
} catch(FIX::SessionNotFound& e) {
this->SetErrorMessage("Failed to send FIX Message");
std::cout << "*** Session not found!" << std::endl;
}
}
Code Snippets I used inside my initiator:
console.log('sleeping for 5 seconds');
sleep(5000)
console.log('done sleeping');
this.send(req, (err) => {
console.log(`ERROR: ${err}`);
});
Definition of sleep:
function sleep(miliseconds) {
var currentTime = new Date().getTime();
while (currentTime + miliseconds >= new Date().getTime()) {
}
}
I checked the nan Execute function in node-quickfix and it is catching a specific exception and not propagating it:
void FixSendWorker::Execute () {
try {
FIX::Session::sendToTarget(*message);
} catch(FIX::SessionNotFound& e) {
std::cout << "*** Session not found!" << std::endl;
}
}
In order to make nan invoke the HandleErrorCallback you should call the SetErrorMessage() function in the catch block of the Execute function. That will cause the error callback on the nodejs side to be called with the error message passed to SetErrorMessage.
I used this post for a source: https://medium.com/#muehler.v/tutorial-to-native-node-js-df4118efb678
I have a function like:
public openCamera = (obj) => {
this._nativeCamera.getPicture()
.subscribe((selectedImage) => {
obj.avatar = selectedImage;
});
};
Now I want to test this function.
it('should pass', function(){
let e = new EventEmitter();
let obj = {};
spyOn(mockNativeCamera,'getPicture').and.callFake(()=>e);
sut.openCamera(obj);
e.emit('Hello Dolly');
expect(obj.avatar).toBe('Hello Dolly'); // This should be checked after subscription has finished, but happens synchronously and thus fails
});
Problem is that although this test should be async, I do not know how to hook to the execution of the asynchronous execution.
I thought of subscribing to the event emitter inside the test, but there is no guarantee which subscription callback will be called first, so this sounds like a bad idea.
Any thoughts?
I'm using Mocha to test a method that has an asynchronous method inside of it. I'm stubbing that dependency with Sinon, and returning a resolved promise. But the promise is never resolved, or at least when the assertion is run it hasn't resolved yet.
Here is the method under test
function identify(traits) {
//THIS GETS CALLED SUCCESSFULLY
userService.get().then(function(user){
//CODE NEVER REACHES HERE
userService.set(user).then(function(){
//do something
}, function(){
//handle error
});
});
}
And here is the test
it('should identify a valid email address', function(){
var user = { email: 'test#example.com' };
var getUserStub = sinon.stub(userService, "get");
var setUserStub = sinon.stub(userService, "set");
var userReturn = { email: 'test#example.com', urls: ['http://some.url.com'] };
getUserStub.returns(Promise.resolve(userReturn));
//THE METHOD UNDER TEST
identifyController.identify(user);
sinon.assert.calledOnce(userService.get); //WORKS FINE
sinon.assert.calledOnce(userService.set); //FAILS
getUserStub.restore();
});
The assertion on userService.set fails, it says it was called 0 times. What am I doing wrong?
I've finally found the problem.
Promises are essentially asynchronous, but sinon calls are synchronous.
See this code pen.
What happens:
You call identifyController.identify(user);
It will call get, which returns a promise, which is asyncrhonous.
The main thread of the program will still be running, so your both sinon.assert.calledOnce calls will happen in sequence, synchronously
By the time get resolves itself and calls set, because promises are non-blocking, the assertion will already have been executed, so it will fail.
So, you can do like this:
function identify(traits) {
return userService.get().then(function(user){
console.log('get');
userService.set(user).then(function(){
console.log('set');
//do something
});
});
}
And change this:
identify(user).then(function(){
sinon.assert.calledOnce(myObj.get); //WORKS FINE
sinon.assert.calledOnce(myObj.set); //WORKS FINE NOW
});
I was trying this method expectAsync2, so there was this question: Why the async test passed, but there are some error messages displayed?
But it seems I didn't use it correctly. Is there any good example of expectAsync2?
In the referenced question expectAsync was just used to guard a async call so that the test doesn't end before the call of new Timer(...) finishes.
You can additionally add provide how often (min/max) the method has to be called to satisfy the test.
If your tested functionality calls a method with more than one parameter you use `expectAsync2)
The mistake in your referenced question was, that your call to expectAsyncX was delayed too.
The call to expectAsyncX has to be made before the async functionality is invoked to register which method has to be called.
library x;
import 'dart:async';
import 'package:unittest/unittest.dart';
class SubjectUnderTest {
int i = 0;
doSomething(x, y) {
i++;
print('$x, $y');
}
}
void main(List<String> args) {
test('async test, check a function with 2 parameters', () {
var sut = new SubjectUnderTest();
var fun = expectAsync2(sut.doSomething, count: 2, max: 2, id: 'check doSomething');
new Timer(new Duration(milliseconds:200), () {
fun(1,2);
expect(sut.i, greaterThan(0));
});
new Timer(new Duration(milliseconds:100), () {
fun(3,4);
expect(sut.i, greaterThan(0));
});
});
}
You can check what happens if you set count and max to 3.
You can have a look at the Asynchronous tests section of the article Unit Testing with Dart.