Testing

Javascript code posted
created at 15 Apr 20:30

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
describe "ToDoApp.Views.TodoCollectionView", ->
  describe "rendering", ->
    describe "when there is a collection", ->
      it "renders the collection", ->
        collection = new ToDoApp.Collections.TodosCollection([
          {id: 1, title: 'make example test', done: false}
          {id: 2, title: 'make example work', done: false}
        ])
        view = new ToDoApp.Views.TodoCollectionView(collection: collection)
        setFixtures(view.render().$el)
        expect(view.children.length).toEqual(2)

    describe "when there is not a collection", ->
      it "renders the collection", ->
        collection = new ToDoApp.Collections.TodosCollection([])
        view = new ToDoApp.Views.TodoCollectionView(collection: collection)
        setFixtures(view.render().$el)
        expect(view.children.length).toEqual(0)

  describe "events", ->
    describe "click .add", ->
      it "adds a new model to the collection", ->
        view = new ToDoApp.Views.TodoCollectionView(collection: new ToDoApp.Collections.TodosCollection())
        setFixtures(view.render().$el)
        $('.add').click()
        expect(view.collection.length).toEqual(1)

      it "sets the new model's title from the text field", ->
        view = new ToDoApp.Views.TodoCollectionView(collection: new ToDoApp.Collections.TodosCollection())
        setFixtures(view.render().$el)
        $('#new-todo').val("This is new")
        $('.add').click()
        expect(view.collection.models[0].get("title")).toEqual("This is new")

      it "clears the value in the text field", ->
        view = new ToDoApp.Views.TodoCollectionView(collection: new ToDoApp.Collections.TodosCollection())
        setFixtures(view.render().$el)
        $('#new-todo').val("This will be cleared")
        $('.add').click()
        expect($('#new-todo').val()).toEqual("")
When we run these tests (on the command line, for ease of copy + paste) we get:

Run all Jasmine suites
Run Jasmine suite at http://localhost:57702/jasmine
Finished in 0.009 seconds
ToDoApp.Views.TodoCollectionView
  rendering
    when there is a collection
       renders the collection
         TypeError: 'undefined' is not a constructor (evaluating 'new ToDoApp.Views.TodoCollectionView({
> [#]             collection: collection
> [#]           })') in http://localhost:57702/assets/spec.js (line 16416)
    when there is not a collection
       renders the collection
         TypeError: 'undefined' is not a constructor (evaluating 'new ToDoApp.Views.TodoCollectionView({
> [#]             collection: collection
> [#]           })') in http://localhost:57702/assets/spec.js (line 16427)
  events
    click .add
       adds a new model to the collection
         TypeError: 'undefined' is not a constructor (evaluating 'new ToDoApp.Views.TodoCollectionView({
> [#]             collection: new ToDoApp.Collections.TodosCollection()
> [#]           })') in http://localhost:57702/assets/spec.js (line 16439)
       sets the new model's title from the text field
        ➤ TypeError: 'undefined' is not a constructor (evaluating 'new ToDoApp.Views.TodoCollectionView({
> [#]             collection: new ToDoApp.Collections.TodosCollection()
> [#]           })') in http://localhost:57702/assets/spec.js (line 16448)
      ✘ clears the value in the text field
        ➤ TypeError: 'undefined' is not a constructor (evaluating 'new ToDoApp.Views.TodoCollectionView({
> [#]             collection: new ToDoApp.Collections.TodosCollection()
> [#]           })') in http://localhost:57702/assets/spec.js (line 16458)
5 specs, 5 failures
Done.
Guard::Jasmine stops server.
rake aborted!
Some specs have failed
3.64 KB in 5 ms with coderay