jsmocktool

Javascript Mock Tool of RSpec Style

Javascript Mock Tool is Mock library of RSpec style.

Internet Explorer - 6+, Firefox - 2+, Chrome - 7+, Opera - 10+, Safari - 3+, Mobile Safari - 3+, Android browser - 2+, Node.js - 0.2+

You can install JSMockTool via NPM:

npm install jsmocktool

But. not yet.

    stub(Obj).should_receive("msg");
    stub("Obj").should_receive("msg");
    //make stub 
    Obj.msg();
stub("Obj",Stub.INSTANCE).should_receive("msg").and_return(value);
new Obj().msg();//value 
mock(Obj).should_receive("msg");
mock("Obj").should_receive("msg");
//make mock 
Obj.msg();
mock("Obj",Mock.INSTANCE).should_receive("msg").and_return(value1);
new Obj().msg(); //value1 
mock("Obj").should_receive("msg").with_param(1,2).and_return(value2);
Obj.msg(1,2); //value2 
mock("Obj").should_receive("msg").with_param(1,2,3).and_raise(new Error("error"));
Obj.msg(1,2,3); //throw new Error("error"); 
mock("Obj").should_receive("msg").with_param(1).and_function(function(1){
        alert(1);
});
Obj.msg(1); //alert(1); 
mock("Foo").should_receive("test").with_param(1,2,mock.anything()).and_return("2");
 
Foo.test(1,2,1); // 2 
Foo.test(1,2,2); // 2 
Foo.test(1,2,3); // 2 
//... 
Foo.test(1,2, (any parameter) ); // 2    
mock("Foo.Bar").should_receive("test").with_param(1,2).and_return("2");
Foo.Bar.test(1,2);
mock("Foo.Bar").verify("test"); //{"total":1,"[1,2]":1}; 
//If you can see error then The verify method call that The mock call not method. 
 
mock("Foo.Bar").should_receive("test").with_param(1,2).and_return("2");
mock("Foo.Bar").verify("test");
//Occour exception 
mock("Foo.Bar").should_receive("test").with_param(1,2).and_return("2");
mock("Foo.Bar").should_receive("test2").with_param(1).and_return("2");
Foo.Bar.test(1,2);
Foo.Bar.test2(1);
Foo.Bar.test2(1);
mock("Foo.Bar").verify_all();
//{"test":{"total":1, "param":{"[1,2]":1}}, "test2":{"total":2, "param":{"[1]":2}}} 
mock("Foo.Bar").should_receive("test").with_param(1,2).and_return("2");
Foo.Bar.test(1,2);
mock("Foo.Bar").verify("test"); //{"total":1,"[1,2]":1}; 
 
mock("Foo.Bar").reset("test");
mock("Foo.Bar").verify("test"); //Occour Exception 
mock("Foo.Bar").should_receive("test").with_param(1,2).and_return("2");
mock("Foo.Bar").should_receive("test2").with_param(1).and_return("2");
Foo.Bar.test(1,2);
Foo.Bar.test2(1);
Foo.Bar.test2(1);
mock("Foo.Bar").verify_all();
//{"test":{"total":1, "param":{"[1,2]":1}}, "test2":{"total":2, "param":{"[1]":2}}} 
 
mock("Foo.Bar").reset_all();
mock("Foo.Bar").verify("test"); //Occour Exception 

The MIT License

Copyright (c) 2011 Arunoda Susiripala

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.