开发者

Jasmine and node.js

开发者 https://www.devze.com 2023-04-10 05:50 出处:网络
Have some Jasmine+Rhino combo to test the javascript code and trying to shift to node.js. However, couldn\'t find any setup instructions on the net (but only this link, with nearly zero instructions).

Have some Jasmine+Rhino combo to test the javascript code and trying to shift to node.js. However, couldn't find any setup instructions on the net (but only this link, with nearly zero instructions). Any help on how to make it true (on Ubuntu) will be highly appreciated.开发者_JAVA技巧


I thought the same thing (regarding documentation) when I first tried to use jasmine-node. As it turns out, though, there's virtually nothing to set up--it works just like RSpec or other testing tools you might be used to. To use Jasmine with your Node project, do the following:

  1. Make sure jasmine-node is installed and that you can run its executable.
  2. Write your specs! I have a sample spec below these steps.
  3. Run your specs with the command jasmine-node specs/ (where specs/ points to the directory with your specs).

That's it! You may find it beneficial to use some sort of build tool, like cake for CoffeeScript or jake.

Here's a quick example of part of a spec from a small project I used jasmine-node on recently; apologies that it's in CoffeeScript. (As an aside: to run jasmine-node against CoffeeScript specs, pass it the --coffee option.)

Chess   = require('../lib/chess')
Board   = Chess.Board
jasmine = require('jasmine-node')

describe "A chess board", ->
  beforeEach ->
    @board = new Board

  it "should convert a letter/number position into an array index", ->
    expect(Board.squares["a1"]).toEqual(0)
    expect(Board.squares["b1"]).toEqual(1)
    expect(Board.squares["a2"]).toEqual(16)
    expect(Board.squares["h8"]).toEqual(119)

  it "should know if an array index represents a valid square", ->
    expect(Board.is_valid_square 0).toBeTruthy()
    expect(Board.is_valid_square 7).toBeTruthy()
    expect(Board.is_valid_square 8).toBeFalsy()
    expect(Board.is_valid_square 15).toBeFalsy()
    expect(Board.is_valid_square 119).toBeTruthy()
    expect(Board.is_valid_square 120).toBeFalsy()
    expect(Board.is_valid_square 129).toBeFalsy()
    expect(Board.is_valid_square -1).toBeFalsy()

  it "should start off clear", ->
    for i in [0..127]
      if Board.is_valid_square(i)
        expect(@board.piece_on(i)).toBeNull()

  describe "#place_piece", ->
    it "should place a piece on the board", ->
      piece = jasmine.createSpy("piece")
      @board.place_piece "a1", piece
      expect(@board.piece_on "a1").toEqual(piece)

    it "should set the piece's location to the given square's index", ->
      piece = jasmine.createSpyObj(Piece, ["position"])
      @board.place_piece "b5", piece
      expect(piece.position).toEqual(65)

[Edit]

You can also add a spec_helper file (with the appropriate extension for your project) at the root of your specs/ directory. Here's the contents of mine, which adds a new matcher to Jasmine:

jasmine = require('jasmine-node')

beforeEach ->
 this.addMatchers
   toInclude: (should_include) ->
     for value in @actual
       return true if value == should_include
     false
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号