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

Is not finding factories that are in tests/factories/ #74

Closed
benlieb opened this issue Apr 16, 2015 · 20 comments
Closed

Is not finding factories that are in tests/factories/ #74

benlieb opened this issue Apr 16, 2015 · 20 comments

Comments

@benlieb
Copy link

benlieb commented Apr 16, 2015

I am having to manually include any factory file that I want to use. I can see the there is some kind of initializer in the source that is supposed to load them, but this doesn't seem to run. It ends up being commented out in my vendor.js

@danielspaniel
Copy link
Collaborator

The factories are only imported if you use the startApp call in your test.
as in this kind of thing:

import Ember from 'ember';
import { make, clearStore } from 'ember-data-factory-guy';
import startApp from '../../helpers/start-app';

var App;

module('User', {
  setup: function() {
    Ember.run(function() {
      App = startApp();
    });
  },
  teardown: function() {
    Ember.run(function() {
      App.destroy();
    });
  }
});

I am guessing you are not doing that, but using moduleForModel kind of test .. right?

@benlieb
Copy link
Author

benlieb commented Apr 16, 2015

Right I'm not doing that. Is there a less "heavy" way of getting all of my factories included?

I'm doing this:

import Ember from 'ember';
import FactoryGuy from 'ember-data-factory-guy';
import '../../../factories/person.js';
import {
moduleForComponent,
test
} from 'ember-qunit';

moduleForComponent('form-inputs/text-field', {
// specify the other units that are required for this test
// needs: ['component:foo', 'helper:bar']
});

test('it renders', function(assert) {

debugger;
//var registration_model = FactoryGuy.build('registration');
var registration_model = FactoryGuy.build('person');

//var component = this.subject({ // creates the component instance
//model: registration_model,
//field: 'fist_name',
////attr: registration_model.first_name
//});

//Ember.run(function() {
//component.set('attr', registration_model.get('first_name'));
//});

//assert.equal(component._state, 'preRender');

//this.render();

//debugger;
//assert.ok(find('input'));
//assert.equal(
//find('input').val(),
//registration_model.get('first_name')
//);

// renders the component to the page
//assert.equal(component._state, 'inDOM');
});

@danielspaniel
Copy link
Collaborator

Yeah .. I hear you on that one. I will cook up a method that will import the factories whenever you want to do that.
For now I write component tests like this

`import Em from 'ember'`
`import { make }  from 'ember-data-factory-guy'`
`import TestHelper from '../helpers/view-test-helper'`
`import startApp from '../helpers/start-app'`

`import {describeComponent} from 'ember-mocha'`

describeComponent 'hsl-input', 'HslInput', ->

App = null
component = null

  beforeEach ->
    Em.run ->
      App = startApp()
      TestHelper.setup()

  afterEach ->
    Em.run ->
      TestHelper.teardown()
      App.destroy()

  hslViewValues = ->
    [
      find('.h input').val(),
      find('.s input').val(),
      find('.l input').val()
    ]

  setupComponent = (scope)->
    component = scope.subject()
    mkt = make('marketing')
    component.set('model', mkt)
    scope.render()

  it "change to color shows in values", ->
    setupComponent(this)
    expect(hslViewValues()).to.arrayEqual([240,100,50])
    mkt.set('color', 'black')
    expect(hslViewValues()).to.arrayEqual([0,0,0])

When I am done with that I will bump up patch version and let you know.

As an aside:

The problem with using moduleForModel ( not true for moduleForComponent ) is that you have to also manually needs: [] every model that is referenced in belongsTo/hasMany .. which gets really tedious.
So, I just don't use those ember test helpers much at all.

@benlieb
Copy link
Author

benlieb commented Apr 16, 2015

Does startApp also set the store? I keep getting: at Object.TestLoader.loadModules (http://localhost:4201/assets/test-loader.js:21:18): Assertion Failed: FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures

I appreciate your feedback and help. Reading the readme using FactoryGuy seemed a simple and easy solution to mocks etc, but I've spent 2 days trying to get this to work and am losing hope.

The get() method in the following line fails:

Ember.run(function() {
component.set('attr', registration_model.get('first_name'));
});

Is this also a startApp issue? If I need to use startApp in all of my tests that use FactoryGuy I can try to figure that out, but an example startApp file implementing this would be really helpful.

@danielspaniel
Copy link
Collaborator

Yes, it does. And take a look at the Sample model test (user-test.js): in the "dummy" application in the ember-data-factory-guy github repo.
the start-app file comes automatically when you use ember-cli.
They create a tests/helpers directory and put that file in there, so you have it too.

@danielspaniel
Copy link
Collaborator

So, to expand a bit. When you starApp() the way I am doing "start the application" FactoryGuy sets the store automatically, and then loads the factories.
You could set the store on the moduleForModel tests ( the helper provides the store ) .. but in the component tests they don't. They create an isolated container that has it's own store, and you have to hack to get it, and then set it into FactoryGuy, which is why I still use the moduleForComponent for example, but startApp() also, to get everything loaded ..

@benlieb
Copy link
Author

benlieb commented Apr 16, 2015

I don't mind doing the startApp, if that's the way you've made things work. I can't remember if you mention to do that in the Readme, but if you make sure to mention that, it will safe some people some headache for sure. Thanks so much for your help.

@danielspaniel
Copy link
Collaborator

Good point, I will add that in the documentation .. so it's less confusing, because I did not really explain that clearly.

@benlieb
Copy link
Author

benlieb commented Apr 16, 2015

Thanks, if you don't mind, I'll run by some other issues I'm having, since others may be having them too, and at this page will be indexed and people might stumble on the solutions :)

This line fails from the docs:

TestHelper.handleUpdate('person', person.id); --> TestHelper is not defined

@danielspaniel
Copy link
Collaborator

Oops, my mistake.. if the comment above I had this:

import TestHelper from '../helpers/view-test-helper'

should have said this:

import TestHelper from 'ember-data-factory-guy/factory-guy-test-helper';

I created a custom TestHelper that overrode the original ( the view-test-helper )

@danielspaniel
Copy link
Collaborator

Also .. you don't have to do that

TestHelper.handleUpdate('person', person.id);

if you have the person model as you do, you can do this:

TestHelper.handleUpdate(person)

@danielspaniel
Copy link
Collaborator

yes.. that last part would not show in 'code' style .. hmm

@danielspaniel
Copy link
Collaborator

:-)

@benlieb
Copy link
Author

benlieb commented Apr 16, 2015

Ok, but TestHelper is not defined

@danielspaniel
Copy link
Collaborator

even with this import:

import TestHelper from 'ember-data-factory-guy/factory-guy-test-helper';

@benlieb
Copy link
Author

benlieb commented Apr 16, 2015

No, with that, it works, but this is what people need in the docs, otherwise that object comes out of nowhere.

@danielspaniel
Copy link
Collaborator

Hmm .. yeah .. I know what your saying. It is in the docs, in the very end ( acceptance test )
But I will improve the docs and make more things clear .. since it obviously is not. Sorry for all the confusion. I know it's annoying to me when I try and use library and I am just beating my head on a wall for an hour.

@benlieb
Copy link
Author

benlieb commented Apr 16, 2015

Thanks for all your help!

@danielspaniel
Copy link
Collaborator

Your welcome, and thanks for taking the time to document the issues. As you say, it helps everyone else too.

@danielspaniel
Copy link
Collaborator

@pixelterra .. I just updated the documentation to show a component test example,
and to explain why the startApp function is necessary.
Hopefully that helps others to get up and running faster.

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