Skip to content

Screw-Unit javascript mocking

by Topper on July 22nd, 2008

I’ll try to blog more about screw-unit javascript testing (since it’s awesome!). However, I just wanted to alert everybody about my fork at github, which will let you mock out various things in your tests.

Let me know what you think.

[js]
//
// DOM mocking
//
TH.insertDomMock(‘some_mock’); // which will insert the file dom_mocks/some_mock.html into a div
//
//
// Object mocking
var someObj = {
foo: function () { return ‘bar’ }
};
someObj.foo() == ‘bar’;
TH.Mock.obj(“someObj”, {
foo: function () { return ‘somethingElse’ }
});
someObj.foo() == ‘somethingElse’; // BUT! Only for this test the next test will have a normal someObj;

//
// Ajax mocking (Prototype.js only)
//
TH.Ajax.mock(“/a_url”, “someText”, 200);
var ajx = new Ajax.Request(“/a_url”, {
onComplete: function (resp) { response = resp }
});
expect(response.responseText).to(equal, “someText”);

[/js]