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

mockFindAll, mockQuery, etc weird behavior with fails #224

Closed
dillonwelch opened this issue Jul 31, 2016 · 14 comments
Closed

mockFindAll, mockQuery, etc weird behavior with fails #224

dillonwelch opened this issue Jul 31, 2016 · 14 comments

Comments

@dillonwelch
Copy link
Contributor

dillonwelch commented Jul 31, 2016

If I have two tests with different arguments to mockQuery (for example, one test makes it return 2 items and the other returns 3), both tests work fine.

When using fails though, the test suite seems to lock on the first assignment. For example, if I have on test do fails({status: 404}) and the other do fails({status: 501}), both tests will fail with 404 (assuming the 404 test executed first). This also happens with success, so if a test with a success mock runs first, both fails tests will not get a failure.

This behavior only happens when using the mock methods directly. If I use TestHelper, it works fine. This happened both in a component and acceptance test.

I was originally on 2.4.4 trying to use mock methods directly. I upgraded to latest (2.7.2) to see if there was a bug fix, and tests that were previously passing with TestHelper started failing for hte same reason once I converted to use the helpers directly.

@danielspaniel
Copy link
Collaborator

This sounds like a pretty good bug.
Let me try and whip up some test cases to show this. If I can't seem to recreate I will see if you can help me make some failing tests.

@danielspaniel
Copy link
Collaborator

Oh well, I think your going to have to write the failing case because I did this:

      mockQuery('user', { name: 'Bob' }).fails();
      mockQuery('user', { name: 'Dan' });

      FactoryGuy.store.query('user', { name: 'Dan' })
        .then(()=> {
          ok(true);
          done();
        });

and this test passed.

So I guess I don't understand how to recreate this bug.
Can you gimme a nice failure case?

@danielspaniel
Copy link
Collaborator

danielspaniel commented Aug 1, 2016

I also tried:

      mockQuery('user', { name: 'Bob' }).fails();
      mockQuery('user', { name: 'Dan' });
     FactoryGuy.store.query('user', { name: 'Bob' }).catch(()=>{
        FactoryGuy.store.query('user', { name: 'Dan' })
          .then(()=> {
            ok(true);
            done();
          });
       });

and that worked fine too

so since your issue is between test runs .. maybe you forgot to call mockTeardown() at the end of each test?
I hope that is the issue, cause otherwise I am a bit stumped. Maybe I can look over your code ( give me access to repo? ) or use ScreenHero so I can check it out?

@dillonwelch
Copy link
Contributor Author

Thanks for the detailed response! My schedule is a bit hectic right now, but sometime this week I will try using mockTeardown() and if that doesn't work I'll try to setup a dummy example or give access to the code.

@dillonwelch
Copy link
Contributor Author

mockTeardown looks to solve the issue for my acceptance tests but not for my component test (since I'm using manualSetup(this.container)).

@danielspaniel
Copy link
Collaborator

can you show me a few of the component tests with setup

@dillonwelch
Copy link
Contributor Author

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import {
  make, makeList, manualSetup, mockQueryRecord, mockQuery
}  from 'ember-data-factory-guy';
import wait from 'ember-test-helpers/wait';
import Ember from 'ember';

let results;

moduleForComponent(
  'index-filter-bar', 'Integration | Component | index filter bar', {
  integration: true,
  beforeEach: function () {
    manualSetup(this.container);

    results = makeList('my-model',
      { name: 'Murphy', other_param: 'a' },
      { name: 'Stella', other_param: 'b' }
    );

    this.set('results', 'cookies');
  }
});

test('it sets the filtering variable during filtering', function(assert) {
  this.set('filtering', 'bananas');
  mockQuery(
    'my-model',
    { start_date: '2013/01/02', end_date: '2014/01/01'}
  ).returns({models: results});

  this.render(hbs`{{index-filter-bar isFiltering=filtering}}`);

  this.$('#filter').click();
  assert.equal(this.get('filtering'), true);

  return wait().then(() =>{
    assert.equal(this.get('filtering'), false);
  });
});

test('it sets componentVariable to [] on a 501 error', function(assert) {
  // TypeError: _this2.get(...).get is not a function
  // unless I change one of the params to be different
  mockQuery(
    'my-model',
    { start_date: '2013/01/02', end_date: '2014/01/01'}
  ).fails({status: 501});

  this.render(hbs`{{index-filter-bar from='2013/02/02' componentVariable=results}}`);

  this.$('#filter').click();

  return wait().then(() =>{
    assert.equal(this.get('results').get('length'), 0);
  });
});

@dillonwelch
Copy link
Contributor Author

Note: This is actually with 2.7.3 now, not 2.7.2

@danielspaniel
Copy link
Collaborator

do what you do for acceptance tests:

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import {
  make, makeList, manualSetup, mockQueryRecord, mockQuery, mockSetup, mockTeardown
}  from 'ember-data-factory-guy';
import wait from 'ember-test-helpers/wait';
import Ember from 'ember';

let results;

moduleForComponent(
  'index-filter-bar', 'Integration | Component | index filter bar', {
  integration: true,
  beforeEach: function () {
    manualSetup(this.container);
    mockSetup();
    results = makeList('my-model',
      { name: 'Murphy', other_param: 'a' },
      { name: 'Stella', other_param: 'b' }
    );

    this.set('results', 'cookies');
  },
  afterEach: function() {
    mockTeardown();
 }
});

@dillonwelch
Copy link
Contributor Author

Nice, that worked.

mockTeardown() is only listed under acceptance tests in the README 😢

@danielspaniel
Copy link
Collaborator

Oops... that was dumb. Feel like giving me a hand and fixing that?

@dillonwelch
Copy link
Contributor Author

👍

@dillonwelch
Copy link
Contributor Author

Unrelated question: is there a way to change the mockjax logging level in manualSetup()?

@danielspaniel
Copy link
Collaborator

I don't think so. I figured if you did mockSetup you would want to set those so I made it so you could do that in mockSetup:

     mockSetup({logLevel: 1, responseTime: 1000, mockjaxLogLevel: 4});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants