chai

Chai - Testing for values in array of objects

我的梦境 提交于 2019-11-30 11:37:22
I am setting up my tests for the results to a REST endpoint that returns me an array of Mongo database objects. [{_id: 5, title: 'Blah', owner: 'Ted', description: 'something'...}, {_id: 70, title: 'GGG', owner: 'Ted', description: 'something'...}...] What I want my tests to verify is that in the return array it conatins the specific titles that should return. Nothing I do using Chai/ Chai-Things seems to work. Things like res.body.savedResults.should.include.something.that.equals({title: 'Blah'}) error out I'm assuming since the record object contains other keys and values besides just title.

chai-as-promised tests don't work with $q promises

喜欢而已 提交于 2019-11-30 08:35:59
问题 I'm trying to get chai-as-promised to work with $q promises with karma unit tests. svc.test = function(foo){ if (!foo){ // return Promise.reject(new Error('foo is required')); return $q.reject(new Error('foo is required')); } else { // get data via ajax here return $q.resolve({}); } }; it.only('should error on no foo', function(){ var resolvedValue = MyServices.test(); $rootScope.$apply(); return resolvedValue.should.eventually.be.rejectedWith(TypeError, 'foo is required'); }); The unit test

Istanbul gives me coverage but ends output with an error

限于喜欢 提交于 2019-11-30 03:15:15
问题 I'm testing out a simple application (from the Mocha tutorial code here https://marcofranssen.nl/using-mocha-chai-sinon-to-test-node-js/) to try to get Istanbul to work. My problem is that Istanbul works fine to give me a coverage summary, but then spits out an error for some reason and I'm not sure why. My tests all pass so they're hopefully not the problem. Here is how I run Istanbul: $ istanbul cover test.js =============================================================================

Testing for specific properties of rejected promises, with Mocha and Chai-as-Promised

旧城冷巷雨未停 提交于 2019-11-30 02:46:03
问题 I am trying to test the specifics of a rejected Promise, using Chai-as-Promised, Mocha, and the "should" dialect. Promises are implemented by bluebird. This works fine: it('it should be rejected when given bad credentials', function () { var promiseOfUsers = db.auth("bad", "credentials").getUsers(); return promiseOfUsers.should.eventually.be.rejectedWith(Error) }); There is a "status" property on that error. I would like to assert that status is 401 This does not work: it('it should be

How does the expect().to.be.true work in Chai? [duplicate]

落爺英雄遲暮 提交于 2019-11-30 01:08:45
问题 This question already has answers here : How does the chai expect function work? (2 answers) Closed 2 years ago . expect(true).to.be.true; In this code, all the 'to', 'be', 'true' seems to be an attribute of the object response from 'expect(true)'. How can these attributes work so that they can raise an exception? 回答1: You can check the source code: [ 'to', 'be', 'been' , 'is', 'and', 'has', 'have' , 'with', 'that', 'which', 'at' , 'of', 'same', 'but', 'does' ].forEach(function (chain) {

Chai - Testing for values in array of objects

梦想的初衷 提交于 2019-11-29 17:09:34
问题 I am setting up my tests for the results to a REST endpoint that returns me an array of Mongo database objects. [{_id: 5, title: 'Blah', owner: 'Ted', description: 'something'...}, {_id: 70, title: 'GGG', owner: 'Ted', description: 'something'...}...] What I want my tests to verify is that in the return array it conatins the specific titles that should return. Nothing I do using Chai/Chai-Things seems to work. Things like res.body.savedResults.should.include.something.that.equals({title:

Chai response.body is always empty {}

删除回忆录丶 提交于 2019-11-29 13:46:22
No matter what my server actually returns, Chai always gives me this exception when I assert response.body: Uncaught AssertionError: expected {} to deeply equal 'test' Even though the actual server response is 'test', not {}: Here is my test: const chai = require('chai'); const chaiHttp = require('chai-http'); const server = require('./test-server'); const should = chai.should(); chai.use(chaiHttp); describe('GET /test', () => { it('it should give test result', (done) => { chai.request(server) .get('/test') .end((err, res) => { console.log(err); // outputs null console.log(res); // outputs

Do I really need to return a promise in test when using Chai as Promised?

时光毁灭记忆、已成空白 提交于 2019-11-29 12:09:43
Chai as Promised documentation states as follows: Notice : either return or notify(done) must be used with promise assertions. And the examples on the site are as follows: return doSomethingAsync().should.eventually.equal("foo"); doSomethingAsync().should.eventually.equal("foo").notify(done); The thing is; I actually wrote a test using chai as promised without returning the promise. Like so: it('should resolve user', function () { $state.get(state).resolve.user(dataservice, { userId: testUser.id }).should.eventually.eq(testUser); $rootScope.$apply(); }); And it works perfectly fine. I am sure

How does the chai expect function work?

送分小仙女□ 提交于 2019-11-29 05:56:54
From chai's api you've got code like this: .exist Asserts that the target is neither null nor undefined. var foo = 'hi' , bar = null , baz; expect(foo).to.exist; expect(bar).to.not.exist; expect(baz).to.not.exist; How does that exist part work? The expect function returns an object, then there's simply a property lookup on the "to" object. That's simply a property evaluation though isn't it? The only thing that would make sense to me is if the exist property is a getter method. What's the go? chai exposes an use method to access the chai export and it's utils . This method can be used by third

Testing JS exceptions with Mocha/Chai [duplicate]

南楼画角 提交于 2019-11-29 02:57:42
This question already has an answer here: Mocha / Chai expect.to.throw not catching thrown errors 6 answers Trying to test some code that throws an exception with Mocha/Chai, but having no luck, here's the simple code I'm trying to test: class window.VisualizationsManager test: -> throw(new Error 'Oh no') Here is my test: describe 'VisualizationsManager', -> it 'does not permit the construction of new instances', -> manager = new window.VisualizationsManager chai.expect(manager.test()).to.throw('Oh no') However, when I run the spec, the test fails and throws the exception. Failure/Error: Oh no