LoginSignup
4
4

More than 5 years have passed since last update.

Jasmine公式サイトのチュートリアルのCoffeeScript版(作り途中)

Last updated at Posted at 2012-12-26
describe "A suite", ->
  it "contains spec with an expectation", ->
    expect(true).toBe true

describe "A suite is just a function", ->
  it "and so is a spec", ->
    a = true
    expect(a).toBe true

describe "The 'tobe' matcher compares with ===", ->
  it "and has a positive case", ->
    expect(true).toBe true

  it "and can have a negative case", ->
    expect(false).not.toBe true

describe "Included matchers:", ->
  it "The 'toBe' matcher compares with ===", ->
    a = 12
    b = a
    expect(a).toBe b
    expect(a).not.toBe null

  describe "The 'toEqual' matcher", ->
    it "works for simple literals and variables", ->
      a = 12
      expect(a).toEqual 12
    it "should work for objects", ->
      foo =
        a: 12
        b: 34
      bar =
        a: 12
        b: 34
      expect(foo).toEqual bar

  it "The 'toMatch' matcher is for regular expressions", ->
    message = 'foo bar baz'
    expect(message).toMatch /bar/
    expect(message).toMatch 'bar'
    expect(message).not.toMatch /quux/

  it "The 'toBeDefined' matcher compares against 'undefined'", ->
    a =
      foo: 'foo'
    expect(a.foo).toBeDefined()
    expect(a.bar).not.toBeDefined()

  it "The 'toBeUndefined' matcher compares against 'undefined'", ->
    a =
      foo: 'foo'
    expect(a.foo).not.toBeUndefined()
    expect(a.bar).toBeUndefined()

  it "The 'toBeNull' matcher compares against null", ->
    a = null
    foo = 'foo'
    expect(null).toBeNull()
    expect(a).toBeNull()
    expect(foo).not.toBeNull()

  it "The 'toBeTruthy' matcher is for boolean casting testing", ->
    a = false
    foo = 'foo'
    expect(foo).toBeTruthy()
    expect(a).not.toBeTruthy()

  it "The 'toBeFalsy' matcher is for boolean casting testing", ->
    a = false
    foo = 'foo'
    expect(a).toBeFalsy()
    expect(foo).not.toBeFalsy()

  it "The 'toContain' matcher is for finding an item in an Array", ->
    a = ['foo', 'bar', 'baz']
    expect(a).toContain 'bar'
    expect(a).not.toContain 'quux'

  it "The 'toBeLessThan' matcher is for mathematical comparisons", ->
    pi = 3.1415926
    e = 2.78
    expect(e).toBeLessThan pi
    expect(pi).not.toBeLessThan e

  it "The 'toBeGreaterThan' is for mathematical comparisons", ->
    pi = 3.1415926
    e = 2.78
    expect(pi).toBeGreaterThan e
    expect(e).not.toBeGreaterThan pi

  it "The 'toBeCloseTo' matcher is for precision math comparison", ->
    # Matcher that checks that the expected item is equal to the actual item
    # up to a given level of decimal precision (default 2).
    pi = 3.1415926
    e = 2.78
    expect(pi).not.toBeCloseTo e, 2
    expect(pi).toBeCloseTo e, 0

  it "The 'toThrow' matcher is for testing if a function throws an exception", ->
    foo = -> 1 + 2
    bar = (a) -> a + 1
    expect(foo).not.toThrow()
    expect(bar).not.toThrow()

describe "A spec", ->
  it "is just a function, so it can contain any code", ->
    foo = 0
    foo += 1
    expect(foo).toEqual 1

  it "can have more than one expectation", ->
    foo = 0
    foo += 1
    expect(foo).toEqual 1
    expect(true).toEqual true

describe "A spec (with setup and tear-down)", ->
  # Here is the same set of specs written a little differently.
  # The variable under test is defined at the top-level scope
  # - the describe block - and initialization code is
  # moved into a beforeEach function.
  # The afterEach function resets the variable before continuing.
  foo = null
  beforeEach ->
    foo = 0
    foo += 1
  afterEach ->
    foo = 0
  it "is just a function, so it can contain any code", ->
    expect(foo).toEqual 1
  it "can have more than one expectation", ->
    expect(foo).toEqual 1
    expect(true).toEqual true

describe "A spec", ->
  # Calls to describe can be nested, with specs defined at any level.
  # This allows a suite to be composed as a tree of functions.
  # Before a spec is executed,
  # Jasmine walks down the tree executing each beforeEach function in order.
  # After the spec is executed, Jasmine walks through the afterEach functions similarly.
  foo = null
  beforeEach ->
    foo = 0
    foo += 1
  afterEach ->
    foo = 0
  it "is just a function, so it can contain any code", ->
    expect(foo).toEqual 1
  it "can have more than one expectation", ->
    expect(foo).toEqual 1
    expect(true).toEqual true
  describe "nested inside a second describe", ->
    bar = null
    beforeEach ->
      bar = 1
    it "can reference both scopes as needed", ->
      expect(foo).toEqual bar

xdescribe "A spec", ->
  # Suites and specs can be disabled with the xdescribe and xit functions, respectively.
  # These suites and specs are skipped when run
  # and thus their results will not appear in the results.
  foo = null
  beforeEach ->
    foo = 0
    foo += 1
  xit "is just a function, so it can contain any code", ->
    expect(foo).toEqual 1

describe "A spy", ->
  # Jasmine’s test doubles are called spies.
  # A spy can stub any function and tracks calls to it and all arguments.
  foo = null
  bar = null
  beforeEach ->
    foo =
      setBar: (value) ->
        bar = value

    spyOn foo, 'setBar'

    foo.setBar 123
    foo.setBar 456, 'another param'

  it "tracks that the spy was called", ->
    expect(foo.setBar).toHaveBeenCalled()

  it "tracks its number of calls", ->
    expect(foo.setBar.calls.length).toEqual 2

  it "tracks all the arguments of its calls", ->
    expect(foo.setBar).toHaveBeenCalledWith 123
    expect(foo.setBar).toHaveBeenCalledWith 456, 'another param'

  it "allows access to the most recent call", ->
    expect(foo.setBar.mostRecentCall.args[0]).toEqual 456

  it "allows access to other calls", ->
    expect(foo.setBar.calls[0].args[0]).toEqual 123

  it "stops all execution on a function", ->
    expect(bar).toBeNull()

ブログやってます:PAPA-tronix !

4
4
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
4
4