标签:
function Hello() {}; Hello.prototype.foo = "foo"; Hello.prototype.bar = null ; Hello.prototype.helloWorld = function() { return "Hello World!"; } Hello.prototype.helloSomeone = function(toGreet) { return this.sayHello() + " " + toGreet; } Hello.prototype.sayHello = function() { return "Hello"; } module.exports = Hello;
npm install -g jasmine
jasmine -v
jasmine init
{ "spec_dir": "spec", "spec_files": [ "**/*[sS]pec.js" ], "helpers": [ "helpers/**/*.js" ], "stopSpecOnExpectationFailure": false, "random": false }
describe("Hello", function () { var Hello = require("../js/Hello"); var hello; beforeEach(function () { hello = new Hello(); }); it("a newly created Hello instance should not be the same instance with the origin one", function () { expect(hello).not.toBe(new Hello()); expect(hello).toEqual(new Hello()); }); describe("helloWorld function", function () { it("should return hello statement", function () { expect(hello.helloWorld()).toBe("Hello World!"); }); it("should contain word ‘World‘", function () { expect(hello.helloWorld()).toContainWord("World!"); }); it("an undefined variable should pass ‘toBeUndefined‘ matcher", function () { expect(hello.a).toBeUndefined(); }); it("a null variable should pass ‘toBeNull‘ matcher", function () { expect(hello.bar).toBeNull(); }); it("variable after boolean casting should pass ‘toBeTruthy‘ ‘toBeFalsy‘ matcher", function () { expect(hello.foo).toBeTruthy(); expect(hello.bar).toBeFalsy(); }); it("should pass the ‘toMatch‘ matcher for regular expressions", function (){ expect(hello.helloWorld()).toMatch(/^\w*\s\w*!$/); }); }); describe("helloSomeone function", function () { it("should calls the sayHello() function", function () { spyOn(hello, "sayHello"); hello.helloSomeone("Chou"); expect(hello.sayHello).toHaveBeenCalled(); expect(hello.sayHello).toHaveBeenCalledTimes(1); }); it("should greet the ‘World‘", function () { spyOn(hello, "helloSomeone"); hello.helloSomeone("World"); expect(hello.helloSomeone).toHaveBeenCalledWith("World"); expect(hello.helloSomeone).not.toHaveBeenCalledWith("world"); }); it("should calls the fake sayHello()", function () { hello.sayHello = jasmine.createSpy("‘sayHello‘ spy"); hello.helloSomeone("world"); expect(hello.sayHello).toHaveBeenCalled(); }); }); });
beforeEach(function () { jasmine.addMatchers({ toContainWord: function () { return { compare: function (actual, expected) { var result = {}; result.pass = (actual.indexOf(expected) !== -1); if( result.pass ) { result.message = "Expected " + actual + " to contain " + expected + "."; } else { result.message = "Expected " + actual + " to contain " + expected + ", but it does not."; } return result; } } } }); });
jasmine
npm install jasmine --save-dev npm install jasmine-spec-reporter --save-dev
var Jasmine = require(‘jasmine‘); var SpecReporter = require(‘jasmine-spec-reporter‘); var noop = function() {}; var jrunner = new Jasmine(); jrunner.configureDefaultReporter({print: noop}); // remove default reporter logs jasmine.getEnv().addReporter(new SpecReporter()); // add jasmine-spec-reporter jrunner.loadConfigFile(); // load jasmine.json configuration jrunner.execute();
node jasmine-runner.js
标签:
原文地址:http://www.cnblogs.com/zhaoxiaoji/p/jasmine-node.html