mardi 25 juillet 2017

mocha test case for res.render not working

I'm writing a simple test case for a route. I've used mocha, chai-http and sinon to check if correct page is rendered or not. This is for contact page where the user provides their info. and contact page is rendered again for now. Below is my test case :

            const chai = require('chai');
            const chaiHttp = require('chai-http');
            const sinon     = require('sinon');
            const ejs = require('ejs');


            var app = require('../app');
            var path = require('path');
            var fs = require('fs');
            var Contact = require('../models/contacts');
            chai.use(chaiHttp);
            var should = chai.should(); 
            var expect = chai.expect;

            describe('Contact', () => {

            describe('/POST Contact', () => {
                it('it should not POST a book without pages field', (done) => {
                    var spy = sinon.spy(ejs, '__express');
                    let contact = {
                        name: "my name",
                        msg: "my msg"
                    }
                    chai.request(app)
                        .post('/contact')
                        .send(contact)
                        .end((err, res) => {
                            console.log(res)
                        //    / console.log(__dirname);


                        // console.log(__filname)
                            // var dir =  __filename + './views/contact.ejs';
                            var dir = path.dirname(fs.realpathSync('./'));;
                            console.log(dir)

                            expect(spy.calledWithMatch(dir+'\views\contact.ejs')).to.be.true;
                            //expect(spy.calledWith(dir)).to.be.true;
                        spy.restore();

                            // Tell Mocha that our test case is done.
                            done();
                            // res.should.have.status(200);
                            //res.should.have.status()
                            // res.should.have.status(200);
                            // res.body.should.be.a('object');
                            //res.body.should.have.property('errors');
                            // res.body.errors.should.have.property('pages');
                            // res.body.errors.pages.should.have.property('kind').eql('required');
                        done();
                        });
                });

            });

            });

The problem is i get an error response on res saying :

     Failed to lookup view "contact" in views directory "C:\\Users\\My User\\Documents\\GitHub\\My Project\\test\\views

So basically it searches for view inside the test directory. Is there any way i can ask to view into the proper directory which is outside test ?

Aucun commentaire:

Enregistrer un commentaire