LoginSignup
68
67

More than 5 years have passed since last update.

Jasmine spec覚え書き

Last updated at Posted at 2012-09-13

Suites and Specs

  • Suite -> describe
  • Spec -> it

JavaScript

describe("abstract suite", function() {
    it("abstract spec", function() {
        //spec
    });
    describe("concrete suite", function() {
        it("concrete spec1", function() {
            //spec
        });
        it("concrete spec2", function() {
            //spec
        });
    });
});

CoffeeScript

describe "abstract suite", ->
    it "abstract spec", ->
        #spec
    describe "concrete suite", ->
        it "concrete spec1", ->
            #spec
        it "concrete spec2", ->
            #spec

beforeEach / afterEach

describe内に書く。RSpecにおけるbefore do ~ end,after do ~ endを思い出す。

JavaScript

beforeEach(function() { 
    //doBefore
});

afterEach(function() {
    //doAfter
});

CoffeeScript

beforeEach ->
    #doBefore
afterEach ->
    #doAfter

Matchers

RSpecにおける.should be_xxxみたいなやつら。

toEqual

  • 同値であるかどうか
expect("string").toEqual("string");

toBe

  • 同オブジェクトであるかどうか
expect("object").toBe("object");

toBeTruthy / toBeFalsy

  • true / false であるかどうか
expect(evaluate).toBeTruthy();
expect(evaluate).toBeFalsy();

toMatch

  • 正規表現でマッチするかどうか
expect('hello and goodbye').toMatch(new RegExp('hello'));

toBeDefined

  • undefinedではないかどうか
var noop = function() {};
expect(noop).toBeDefined();

toBeNull

  • nullであるかどうか
expect(arg).toBeNull();

toContain

  • 配列に要素が、または文字列に文字列が含まれているかどうか
expect([1,2]).toContain(1);

toBeLessThan / toBeGreaterThan

  • 数値的に少ないかどうか / 多いかどうか
expect(arg).toBeLessThan();
expect(arg).toBeGreaterThan();

toThrow

  • 例外が投げられるかどうか
var err = function() {
    throw new TypeError;
};
expect(err()).toThrow();

.not

toXXXの頭に付けて否定

expect("object").not.toEqual("otherObject");
68
67
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
68
67