 
 
 
+    describe "View creation", ->
+        describe "Creation of the first view + design document creation", ->
+            before cleanRequest
+
+            it "When I send a request to create view every_docs", (done) ->
+                map = (doc) ->
+                    emit doc._id, doc
+                    return
+                @viewAll = {map:map.toString()}
+
+                client.put 'request/every_docs/', @viewAll, \
+                        (error, response, body) =>
+                    response.statusCode.should.equal 200
+                    done()
+            
+            it "Then the design document should exist and contain the view", \
+                    (done) ->
+                db.get '_design/cozy-request', (err, res) ->
+                    should.not.exist err
+                    should.exist res
+                    res.views.should.have.property 'every_docs', @viewAll
+                    done()
+
+        describe "Creation of a new view", ->
+            before cleanRequest
+
+            it "When I send a request to create view even_num", (done) ->
+                map = (doc) ->
+                    emit doc._id, doc if (doc.num? && (doc.num % 2) is 0)
+                    return
+                @viewEven = {map:map.toString()}
+
+                client.put 'request/even_num/', @viewEven, \
+                        (error, response, body) =>
+                    response.statusCode.should.equal 200
+                    done()
+
+            it "Then the design document should exist and contain the views", \
+                    (done) ->
+                db.get '_design/cozy-request', (err, res) ->
+                    should.not.exist err
+                    should.exist res
+                    res.views.should.have.property 'every_docs', @viewAll
+                    res.views.should.have.property 'even_num', @viewEven
+                    done()
+
     describe "Access to a view without option", ->
         describe "Access to a non existing view", ->
             before cleanRequest

                    done()

            it "Then error 404 should be returned", ->
                @response.statusCode.should.equal 404
        
        describe "Access to an existing view : no_doc", ->
            before cleanRequest

            it "When I send a request to access view no_doc", (done) ->
                client.get "request/no_doc/", (error, response, body) =>
                    response.statusCode.should.equal 200
                    @body = parseBody response, body
                    done()

            it "Then I should have no document returned", ->
                @body.should.be.empty

        describe "Access to an existing view : every_docs", (done) ->
            before cleanRequest

            it "Then I should have 101 documents returned", ->
                @body.should.have.length 101

        describe "Access to an existing view : even_num", (done) ->
            before cleanRequest

            it "When I send a request to access view every_docs", (done) ->
                client.get "request/even_num/", (error, response, body) =>
                    response.statusCode.should.equal 200
                    @body = parseBody response, body
                    done()

            it "Then I should have 51 documents returned", ->
                @body.should.have.length 51

    describe "Update of an existing view", ->
        describe "Redefinition of existing view even_num", ->
            before cleanRequest

            it "When I send a request to create view even_num", (done) ->
                map = (doc) ->
                    emit doc._id, doc if (doc.num? && (doc.num % 2) isnt 0)
                    return
                @viewEven = {map:map.toString()}

                client.put 'request/even_num/', @viewEven, \
                        (error, response, body) =>
                    response.statusCode.should.equal 200
                    done()

            it "Then the design document should exist and contain the views", \
                    (done) ->
                db.get '_design/cozy-request', (err, res) ->
                    should.not.exist err
                    should.exist res
                    res.views.should.have.property 'every_docs', @viewAll
                    res.views.should.have.property 'even_num', @viewEven
                    done()

            it "And I should retrieve the good values", (done) ->
                client.get "request/even_num/", (error, response, body) =>
                    response.statusCode.should.equal 200
                    @body = parseBody response, body
                    @body.should.have.length 50
                    done()
