68
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Jasmine spec覚え書き

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");

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
68
Help us understand the problem. What are the problem?