misprintt/mockatoo — pull request #8 by misprintt (2012-11-01 11:40:39+00:00)
Title: Property mocking

### Mocking Properties that are read or write only

Examples:

Mockatoo.when(mock.someProperty).thenReturn("hello");
Mockatoo.when(mock.someSetter).thenThrow("exception");
Mockatoo.when(mock.someGetter).thenThrow("exception");
Mockatoo.when(mock.someGetter).thenCall(function(){return "foo"});

A property that is read/write only, or calls out to getter/setter functions
can be stubbed to a specific return value:

Mockatoo.when(mock.someReadOnlyProperty).thenReturn("foo");

If a property has a getter function, stubbing a return value will automatically
stub the underlying getter method - the equivalent of:

Mockatoo.when(mock.get_someReadOnlyProperty()).thenReturn("foo");

Getters can also be stubbed to a custom callback:

Mockatoo.when(mock.someGetter).thenCall(f);

Both Getters and Setters can also be stubbed with an exception:

Mockatoo.when(mock.someGetter).thenThrow("foo");
Mockatoo.when(mock.someSetter).thenThrow("foo");

var result = mock.someGetter;//throws exception
mock.someSetter = "a";//throws exception

>Note: If a property has both a getter and setter, both getting and setting the
property with trigger the exception

There are some limitations to property stubbing:

* stubbed properties cannot be chained (thenReturn("foo", "bar", "etc"))
* only getters support stubbed callbacks (`thenCall(f)`)
* only getter and setters support stubbed exceptions (`thenThrow(xxx)`)

### Setting default mock for spied methods

If you just want to stub a single method using the default mock values then

Mockatoo.when(mock.someMethod()).thenMock();
mock.someMethod();//returns default mock value (usually null)