Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling setCreateResults/clearResults in multiple test files #16

Closed
mabashian opened this issue Dec 5, 2014 · 1 comment
Closed

Calling setCreateResults/clearResults in multiple test files #16

mabashian opened this issue Dec 5, 2014 · 1 comment
Labels

Comments

@mabashian
Copy link

I have two different Models that I am trying to test.

cats.spec.js:

var wolfpack = require('wolfpack');
var sinon = require('sinon');
var assert = require('assert');

global.Cat= wolfpack('../../../api/models/Cat');

wolfpack.clearResults();

wolfpack.setFindResults({
    catId: 1,
    type: 'Siamese'
});

describe('Cat Model', function () {
    describe('example test', function() {
        it ('should do something', function(done) {
            Cat.findOne(1).then(function(results){
                //results always contains the results set in the dog test
            });
        });
    });
});

dog.spec.js:

var wolfpack = require('wolfpack');
var sinon = require('sinon');
var assert = require('assert');

global.Dog= wolfpack('../../../api/models/Dog');

wolfpack.clearResults();

wolfpack.setFindResults({
    dogId: 1,
    type: 'Lab'
});

describe('Dog Model', function () {
    describe('example test', function() {
        it ('should do something', function(done) {
            Dog.findOne(1).then(function(results){
                //results contain the correct dog object
            });
        });
    });
});

It looks to me like when I try to run the test:

  1. The results are cleared
  2. Cat find results are set.
  3. The results are cleared
  4. Dog find results are set.

Then the tests are executed and only the Dog results are available in each test. Am I going about this the wrong way? When should I be setting the find results and when should I be clearing them? I want to define results for the Cat model and then have them available while I test the Cat Model. Then when I test the Dog model I want to clear the results, reset them and continue testing.

@fdvj
Copy link
Owner

fdvj commented Dec 11, 2014

Hey @mabashian. The issue you are having is because your code executes asynchronously. Even though you set the results correctly, wolfpack cannot determine which promise is fulfilled first, so by the time the Cat promise executes, the clearResults for the Dog already executed and the setFindResults for it as well. In this case, you must clear and set the results asynchronously as well for the tests

beforeEach(function(done){
  wolfpack.clearResults();

  wolfpack.setFindResults({
    dogId: 1,
    type: 'Lab'
  });  
  done();
});

You can also set the results directly above the Dog.findOne lines.

@fdvj fdvj added the question label Dec 26, 2014
@fdvj fdvj closed this as completed Dec 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants