Specifies the arguments of an expected method call. For example, the following creates
the expectation that the method appendFileSync() will be called with the arguments
"log.txt" and "This is a log entry.":
mock->fs.expects("appendFileSync").args("log.txt","This is a log entry.")
args() should be called with one or more arguments. Do not use args() if an expected
method is to be called with no arguments:
mock->socket = newSocket()socket.expects("close")# ok; no arguments expected (e.g. socket.close() )socket.expects("close").args()# not ok; do not use args() with no arguments
args() should only be called once per expectation:
mock->fs.expects("appendFileSync").args("l.txt",0)# okfs.expects("appendFileSync").args("l.txt").args(0)# not okfs.expects("appendFileSync").args("l1.txt",1).args("l2.txt",2)# not ok ...fs.expects("appendFileSync").args("l1.txt",1)# ... do this insteadfs.expects("appendFileSync").args("l2.txt",2)
args() returns an expectation object upon which either returns() or throws() can be
called. args() should not be called after returns() or throws():
mock->socket = newSocket()socket.expects("greet").args("hello").returns("hi")# oksocket.expects("greet").returns("hi").args("hello")# not oksocket.expects("greet").args(null).throws("error")# oksocket.expects("greet").throws("error").args(null)# not ok
Example:
chai = require("chai")
should = chai.should()
mock = require("TinyMock")
Chat = require("../src/Chat")
describe "Chat", ->
describe "greet(my_name)", ->
it "sends a greeting and returns the response", ->
mock (socket) ->
socket.expects("write").args("Hello, my name is Aristotle.\n")
socket.expects("read").returns("Greetings Aristotle!\n")
chat = new Chat(socket)
chat.greet("Aristotle").should.equal("Greetings Aristotle!\n")
Creates an expectation that method_name will be called on object. For example, the following
creates the expectation that the method appendFileSync() will be called on the object fs:
mock->fs.expects("appendFileSync")
expects() can be called on any class, on any instance of Object, or on anything that inherits from Object:
mock->Object.expects("toString")# Object is a classclassMyClass# emptymock->MyClass.expects("toString")# any class that inherits from Object (i.e. any class)mock->object = newObject()object.expects("toString")# object is an instance of ObjectclassMyClass# emptymock->object = newMyClass()object.expects("toString")# object is an instance of MyClass, which inherits from Objectmock->object = Object.create(null);object.expects("my_method")# not ok; object has no method 'expects'
method_name must already exist unless expects() is called on a mock object passed in by mock():
classMyClass# emptymock->MyClass.expects("my_method")# not ok; my_method() is not an existing method of MyClassclassMyClass# emptymock->object = newMyClass()object.expects("my_method")# not ok; my_method() is not an existing method of MyClassmock(object) ->object.expects("my_method")# ok; object was passed in by mock()
For mock objects not passed in by mock, expects() replaces the existing method_name with
a mock method, therefore the original method_name's functionality is lost from the point
the expects() was called to the end of the callback function:
classMyObjectmy_method: ->console.log("hello")object = newMyObject()mock->object.my_method()# prints "hello" on the consoleobject.expects("my_method")object.my_method()# does nothingobject.my_method()# prints "hello" on the console
expects() returns an expectation object upon which the args(), returns(), or throws() methods can be called.
This allows for a bit of fluency:
chai = require("chai")
should = chai.should()
mock = require("TinyMock")
Chat = require("../src/Chat")
describe "Chat", ->
describe "greet(my_name)", ->
it "sends a greeting and returns the response", ->
mock (socket) ->
socket.expects("write").args("Hello, my name is Aristotle.\n")
socket.expects("read").returns("Greetings Aristotle!\n")
chat = new Chat(socket)
chat.greet("Aristotle").should.equal("Greetings Aristotle!\n")
Use mock() to run a callback function that sets expectations and runs tests:
mock-># callback functionfs.expects("appendFileSync")# set an expectation...mockmock_count: 100,(books...) -># the number of mock objects to create (default is 5)book.expects("rate")forbookinbooks...mockexpects_method_name: "expects2",-># use a different name for expects()obj = newMyClass()obj.expects2("start")...
mock() adds expects() to Object.prototype so that you can add an expectation on anything that is an instance
of Object or that inherits from Object. When the callback function finishes running, mock() verifies that all
expectations were actually met. It also restores all original methods that were replaced with mock methods
by calls to expects(), and removes expects() from Object.prototype.
mock() also pre-creates mock objects and passes them to the callback function:
Note that you can only add an expectation for a non-existent method to the mock objects passed in by mock().
The following will not work because "my_method" is not an existing method of obj (an Object), and obj was
not passed in by mock():
mock->obj = newObject()obj.expects("my_method")# throws "'my_method' not an existing method" error...
This prevents you from accidentally mocking a non-existing method on
an existing object, which can be painful to track down:
mock->fs.expects("appnedFileSync")# spelling mistake ('append' not 'appned')fs.expects("noSuchMethod")# method does not exist on fschat = newChat()chat.greet("Aristotle")
By default mock() creates five mock objects and passes them to the callback function. You can use
as few or as many of them as appropriate, and of course you can name them any way you like:
Sometimes you may find you want to set expectations on an object that already has an expects()
method. In such cases, you can tell mock to use a different name for expects():
classMyClassstart: ->"something"expects: ->"anything"mockexpects_method_name: "expects2",-># use expects2() instead of expects() ...obj = newMyClass()obj.expects2("start")# ... because MyClass already has an expects() method...
WARNING: watch the space before the callback argument list's opening parenthesis:
# this is correctmock(m1) ->...# but this is notmock(m1) ->...
In summary, mock() creates mock objects, does verification, and cleans up so that you don't have to.
The price is an extra level of indentation.
Use the Node.js require()
function to make TinyMock available in code:
mock = require("TinyMock")
Example:
chai = require("chai")
should = chai.should()
mock = require("TinyMock")
Chat = require("../src/Chat")
describe "Chat", ->
describe "greet(my_name)", ->
it "sends a greeting and returns the response", ->
mock (socket) ->
socket.expects("write").args("Hello, my name is Aristotle.\n")
socket.expects("read").returns("Greetings Aristotle!\n")
chat = new Chat(socket)
chat.greet("Aristotle").should.equal("Greetings Aristotle!\n")
Specifies the value that a mock method installed by an expectation should return. For
example, the following creates an expectation for the method read() and that the
mock read() installed by the expectation should return "a message":
returns() should be called with one argument. Do not use returns() if the mocked
method should not return anything:
mock->socket = newSocket()socket.expects("close")# ok; mocked close() does not return anythingsocket.expects("close").returns()# not ok; do not use returns() with no arguments
returns() should only be called once per expectation:
mock->socket = newSocket()socket.expects("read").returns("a message")# oksocket.expects("read").returns("m1").returns("m2")# not okmock->socket.expects("read").returns("m1")socket.expects("read").returns("m2")# not ok; TinyMock cannot handle this
returns() and throws() cannot be used on the same expectation:
mock->socket.expects("read").throws("an error").returns("a message")# not ok
Example:
chai = require("chai")
should = chai.should()
mock = require("TinyMock")
Chat = require("../src/Chat")
describe "Chat", ->
describe "greet(my_name)", ->
it "sends a greeting and returns the response", ->
mock (socket) ->
socket.expects("write").args("Hello, my name is Aristotle.\n")
socket.expects("read").returns("Greetings Aristotle!\n")
chat = new Chat(socket)
chat.greet("Aristotle").should.equal("Greetings Aristotle!\n")
Specifies the error that a mock method installed by an expectation should throw. For
example, the following creates an expectation for the method read() and that the
mock read() installed by the expectation should throw "an error":
throws() should be called with one argument. Do not use throws() if the mocked
method should not throw anything:
mock->socket = newSocket()socket.expects("close")# ok; mocked close() does not throw anythingsocket.expects("close").throws()# not ok; do not use throws() with no arguments
throws() should only be called once per expectation:
mock->socket = newSocket()socket.expects("read").throws("e")# oksocket.expects("read").throws("e1").throws("e2")# not okmock->socket.expects("read").throws("e1")socket.expects("read").throws("e2")# not ok; TinyMock cannot handle this
throws() and returns() cannot be used on the same expectation:
mock->socket.expects("read").returns("a message").throws("an error")# not ok
Example:
chai = require("chai")
should = chai.should()
mock = require("TinyMock")
Chat = require("../src/Chat")
describe "Chat", ->
describe "greet(my_name)", ->
it "sends a greeting and returns the response", ->
mock (socket) ->
socket.expects("write").args("Hello, my name is Aristotle.\n")
socket.expects("read").throws(new Error("socket read error"))
chat = new Chat(socket)
(-> chat.greet("Aristotle") ).should.throw("socket read error\n")