Domanda

I'm currently trying to write an application using YUI3 and via the TDD methodology. I'm running into an issue where I can't figure out how to properly write tests to test the code that leverages YUI3 Y.io.

According to the YUITest docs, they talk about Mock Objects and the XMLRequestObject. Which is essentially what I'm trying to Mock out. But I can't figure out how to do it when I'm using Y.io instead of XMLRequestObject.

Has anyone encountered this before?

È stato utile?

Soluzione

You can tell Y.IO to use a custom transport other than the default XMLRequestObject. There may be other ways to add custom transports to Y.IO, but this worked for me:

Create a function that monkey patches a custom transport mode, mock, to Y.IO:

function _createMockTransport(mockXHR) {
    Y.IO.transports.mock = function () {
        return mockXHR;
    }
}

Then, in your setup function, add a variant of the following:

// I change the Y.IO instantiation configuration when instantiating my application
this.application = new Application({ yIOConfig: { xdr: { use: 'mock' }}})

this.xhr = Y.Mock();
Y.Mock.expect(this.xhr, {
    method: 'send',
    args: [Y.Mock.Value.Object, '/foo', Y.Mock.Value.Object]
});

_createMockTransport(this.xhr);

A example test would look like this:

"test /foo": function () {
    this.appliation.doServerCall();
    Y.Mock.verify(this.xhr);
},
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top