문제

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?

도움이 되었습니까?

해결책

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);
},
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top