LaszloMock provides Mock Objects for OpenLaszlo LzUnit tests.
LazloMock lets you generate mock objects on the fly from LZX classes. Mock objects allow interaction-based unit testing and can improve object decoupling.
See our work on the Presenter First design pattern for decoupling GUI programming for unit testing. Mock Objects are very helpful in supporting the development and testing of Model View Presenter code triads. Our introduction to development for Rich Clients explains all of the techniques that drove our desire to develop LaszloMock.
Note that LaszloMock was written for use with OpenLaszlo 3.3.3. While it has been reported to work fine with OL4, it may not work with other releases.
LaszloMock is open source software available under the terms of the Common Public License.
» Get Started with LaszloMock Tutorial
» Download
[ZIP] LaszloMock library (includes ‘SimpleExample’ shown below and ‘ParkRangerExample’)
Please let us know if you have any problems or questions.
Simple Example
The following is a complete working sample of LzUnit tests with LaszloMock objects. An additional example is also included in the download archive link above.
Please see Get Started with LaszloMock for a step-by-step LaszloMock tutorial.
<canvas debug="true">
<include href="LaszloMock.lzx"/>
<include href="lzunit"/>
<!-- A sample class that we will mock out below -->
<class name="Foo">
<event name="bazzed"/>
<method name="bar"></method>
<method name="bar2" args="thing1, thing3"></method>
<method name="bar3"></method>
</class>
<!-- A sample class that is composed of our class to mock.
This represents the class you are testing. -->
<class name="UserOfFoo">
<method event="onconstruct"><![CDATA[
this.foo = foo;
]]>
</method>
<method name="someMethod"><![CDATA[
foo.bar();
]]>
</method>
<method name="methodThatCreatesBlock"><![CDATA[
var block = function() {
foo.bar2(1, 2);
foo.bar3("hello world");
}
foo.bar(block);
]]>
</method>
<!-- A handler so that our sample event is created -->
<method event="bazzed" reference="foo"></method>
</class>
<class name="LaszloMockTest" extends="TestCase">
<method name="setUp"><![CDATA[
// Create a Mock from a given class prototype.
this.fooMock =
new LaszloMock(this, {classToMock: Foo.prototype});
this.userOfFoo = new UserOfFoo(this, {foo: fooMock});
]]>
</method>
<method name="tearDown"><![CDATA[
// Verify the mock in teardown or at the end of your test.
// Verify returns true/false if the mock passed/failed.
assertTrue(fooMock.verifyMock());
]]>
</method>
<method name="testUsageExamples"><![CDATA[
// Call methods on your mock like you would on any
// Laszlo Object. Arguments will be matched if they
// are simple types. Objects will be matched only by
// calling typeOf()
fooMock.bar();
fooMock.bar2("argument1", 123.42);
// If your method needs to return a value call
// andReturn("return value")
fooMock.bar3().andReturn("any return object");
// If you need to add a method to the mock call the
// addMockMethod("method name")
fooMock.addMockMethod("newMethodBar4");
// If you want your mock to fire an event call the
// fireMockEvent("eventName");
// fireMockEvent returns true/false if the event was there
// (aka an object was listening for the event)
assertTrue(fooMock.fireMockEvent("bazzed"));
// If you want your mock to fire an event with arguments call
// the fireMockEvent("eventName", expectedArgs);
// with an array that represents the expectedArguments
var eventArgs = ["arg1", "arg2"];
assertTrue(fooMock.fireMockEvent("bazzed", eventArgs));
// If you need to reset the mock and clear any calls
// to it call resetMock(); This will clear out all methods
// called on the mock or expectations that the mock has
// had set on it.
fooMock.resetMock();
// someMethod() will now act like it hasn't been called
]]>
</method>
<method name="testSomeMethod"><![CDATA[
// Here we have a typical simple test that
//a method was called on the composed object.
// This line sets the call expectation
fooMock.bar();
// This line tells the mock to go into
// replay mode (stop accepting expectations).
fooMock.replayMock();
// This line calls our method under test
// (which calls our mock's method)
userOfFoo.someMethod();
// The verify() method above handles the mock verification.
]]>
</method>
<method name="testAndStubUsage"><![CDATA[
var capturedBlock = null;
// andStub can take in a block (anonymous function, closure)
// that will be executed during the tests
fooMock.bar(function() {}).andStub(function(block) {
capturedBlock = block;
assertNotNull(capturedBlock);
});
fooMock.bar2(1, 2);
fooMock.bar3("hello world");
fooMock.replayMock();
userOfFoo.methodThatCreatesBlock();
capturedBlock.call();
]]>
</method>
</class>
<TestSuite>
<LaszloMockTest />
</TestSuite>
</canvas>
Copyright © Atomic Object LLC. - Grand Rapids, MI 49506 - (616) 776-6020 - Contact Us
