Test case throws an "Uncaught AssertError"

14 views
Skip to first unread message

Vitali S.

unread,
Mar 21, 2013, 6:51:49 AM3/21/13
to js-test...@googlegroups.com
Hi,

I'm collecting my first experience with JsTestDriver in combination with WebStorm 5.
Now I reached my limit and need help.

Gateway_Test.prototype.test_sendRequest = function() {
   
    // get the gateway
    var gateway = Service.LogService.gateway;
    // register the LogService for logging
    gateway.registerService("LogService", Diagnostics.LogProvider);

    // Open the gateway
    gateway.onOpen(null);
    // Register gateway ready handler
    Gateway.GatewayProvider.registerReadyHandler(isReady);

    function isReady() {
        console.log("Gateway status: Ready!");
        sendMessage();
    }

    function sendMessage() {
        // Prepare and send message
        var msg = JSON.parse('{"service":"TestService"}');
        console.log("Prepared message before send to gateway: " + JSON.stringify(msg));
        // send the message and get the manipulated message back
        var outgoingMsg = gateway.sendRequest(msg);
        // provoke error
        outgoingMsg.callbackIdx = undefined;
        console.log("Manipulated message after send to gateway: " + JSON.stringify(outgoingMsg));
        // Expect that callbackIdx is not undefined
        assertNotUndefined("CallbackIdx Check", outgoingMsg.callbackIdx);
    }
};


The test case is passed (should fail) and the following messages are shown in the Chrome console:

  1. Uncaught AssertError: CallbackIdx Check expected not undefined but was undefined runner.js:682
    1. assertNotUndefinedrunner.js:978
    2. fireConnectionReadyGateway.js:219
Gateway(onClose): bridge closed: '[object CloseEvent]'. Diagnostics.js:40
Gateway(fireConnectionLost) Diagnostics.js:40
Gateway.GatewayProvider(onClose) Diagnostics.js:40
Gateway.GatewayProvider(fireClose)


1. Why does the test pass?
2. Why do I only see in the console an error?

Thanks

Vitali

Cory Smith

unread,
Mar 21, 2013, 10:23:40 AM3/21/13
to js-test...@googlegroups.com
Because your code is executing /after/ the test finishes.

To test asynchronous callbacks you need to use the
https://code.google.com/p/js-test-driver/wiki/AsyncTestCase.
> --
> You received this message because you are subscribed to the Google Groups
> "JsTestDriver" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to js-test-drive...@googlegroups.com.
> To post to this group, send email to js-test...@googlegroups.com.
> Visit this group at http://groups.google.com/group/js-test-driver?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Vitali S.

unread,
Mar 21, 2013, 11:11:04 AM3/21/13
to js-test...@googlegroups.com
Many thanks. I saw this page some times but did not know how to realize or adapt it to my code.
Now I got it.

Here is my working modified code:

var Gateway_sendRequest_Test = AsyncTestCase("Test the method sendReuest");
Gateway_sendRequest_Test.prototype.test_sendRequest = function(queue) {

    // get the gateway   
    var gateway = Service.LogService.gateway;

    queue.call('Step 1: open the gateway', function() {

       

        // register the LogService for logging
        gateway.registerService("LogService", Diagnostics.LogProvider);

        // Open the gateway
        gateway.onOpen(null);
        // Register gateway ready handler
        Gateway.GatewayProvider.registerReadyHandler(isReady);

        function isReady() {
            console.log("Gateway status: Ready!");
        };
    });

    queue.call('
Step 1: send the request', function() {

        // Prepare and send message
        var msg = JSON.parse('{"service":"TestService"}');
        console.log("Prepared message before send to gateway: " + JSON.stringify(msg));
        // send the message and get the manipulated message back
        var outgoingMsg = gateway.sendRequest(msg);
        // Test manipulation to see what will happen on assert error

        outgoingMsg.callbackIdx = undefined;
        console.log("Manipulated message after send to gateway: " + JSON.stringify(outgoingMsg));
        // Expect that callbackIdx is not undefined
        assertNotUndefined("CallbackIdx Check", outgoingMsg.callbackIdx);
    });
};

Vitali S.

unread,
Mar 21, 2013, 11:25:31 AM3/21/13
to js-test...@googlegroups.com
This would make more sense:

    // get the gateway   
    var gateway = Service.LogService.gateway;
    var outgoingMsg;

    queue.call('Step 1: open the gateway', function() {

        // register the LogService for logging
        gateway.registerService("LogService", Diagnostics.LogProvider);

        // Open the gateway
        gateway.onOpen(null);
        // Register gateway ready handler
        Gateway.GatewayProvider.registerReadyHandler(isReady);

        function isReady() {
            console.log("Gateway status: Ready!");
            sendMessage();
        };
       
        function sendMessage() {
            // Prepare and send message
            var msg = JSON.parse('{"service":"TestService"}');
            console.log("Prepared message before send to gateway: " + JSON.stringify(msg));
            // send the message and get the manipulated message back
            outgoingMsg = gateway.sendRequest(msg);
            // Test manipulation to see what will happen on assert error

            outgoingMsg.callbackIdx = undefined;
            console.log("Manipulated message after send to gateway: " + JSON.stringify(outgoingMsg));
        }
    });

    queue.call('
Step 1: send the request', function() {

        // Expect that callbackIdx is not undefined
        assertNotUndefined("CallbackIdx Check", outgoingMsg.callbackIdx);
    });
};


Am Donnerstag, 21. März 2013 11:51:49 UTC+1 schrieb Vitali S.:
Reply all
Reply to author
Forward
0 new messages