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

Integration tests bypassing store and making server requests. #27

Closed
tmcgilchrist opened this issue Oct 28, 2014 · 7 comments
Closed

Integration tests bypassing store and making server requests. #27

tmcgilchrist opened this issue Oct 28, 2014 · 7 comments

Comments

@tmcgilchrist
Copy link

I'm writing integration style tests for my embercli project and have come across an issue where I think I've stubbed correctly but still see network requests.

This is my integration spec which visits a particular route that loads all the album models so I've created a bunch of albums via store.makeList('album', 5) which I was expecting to prevent any real ajax calls being made.

Despite this setup I still see GET http://localhost:4200/api/albums 404 (Not Found) when I run the embercli tests in a browser. I've tried using the handleFind function but that seems targeted towards an individual model and doesn't work either.

Am I missing part of the API that covers stubbing out things like @store.find('albums') which loads all albums? It seems like those calls bypass anything in the store and always try to make an ajax call.

Any help would be appreciated. Other than this issue I'm really liking this library, seems to do most of the things I want and makes it very easy. Cheers.

`import Ember from 'ember'`
`import startApp from '../helpers/start-app'`
`import albumFactory from '../fixtures'`

App = null
testHelper = null
store = null
TestHelper = Ember.Object.createWithMixins(FactoryGuyTestMixin)

module 'Acceptance: Dashboard',
  setup: ->
    App = startApp()
    testHelper = TestHelper.setup(App)
    store = testHelper.getStore()
    albums = store.makeList('album', 5)

  teardown: ->
    Ember.run -> testHelper.teardown()
    Ember.run App, 'destroy'

test 'visiting /dashboard', ->
  visit '/dashboard'

  andThen ->
    equal currentPath(), 'dashboard'

test 'displays top albums', ->
  visit '/dashboard'

  andThen ->
    equal(find('.top-albums li').length, 5, "Displays the top 5 albums")

My model definition

`import DS from 'ember-data'`

Album = DS.Model.extend
  name: DS.attr('string')
  artist: DS.attr('string')
  coverImage: DS.attr('string')
  year: DS.attr('date')
  playCount: DS.attr('number', {defaultValue: 0})
  tracks: DS.hasMany('track', { async: true })

`export default Album`

and the fixtures file.

FactoryGuy.define('album', {
  sequences: {
    albumName: (num) ->
      'Album' + num

    artistName: (num) ->
      'Artist' + num
  },
  default: {
    name: FactoryGuy.generate('albumName'),
    artist: FactoryGuy.generate('artistName')
  }
});

`export default {}`
@danielspaniel
Copy link
Collaborator

Ooo .. yes, forgot to add a convenience method ( and or documentation ) to handle that case ( for loading many models like that .. )
What version of ember-data are you using? ( not too relevant but curious )

You could do something like:

     responseJson = FactoryGuy.buildList('album', 5)
     testHelper.stubEndpointForHttpRequest( '/api/albums',  albums: responseJson )

And this would replace:

     albums = store.makeList('album', 5)

and that should do it .. ( it's what I do in my own tests )
but let me know if this does not work.

@tmcgilchrist
Copy link
Author

We're using Ember 1.7.0 and EmberData 1.0.0-beta.10 at the moment, will likely update once I get some time.

That works perfectly thanks.

Slightly off topic but the instructions for adding this to an embercli project aren't that clear, initially I had some issues around jquery.mockjax not getting pulled in via either npm or bower.

@danielspaniel
Copy link
Collaborator

Great .. glad you are all set with that.
Hmm ... ok .. your comments about mockjax .. remind me that maybe I should just bundle that in the ember-data-factory-guy dist file so there is no confusion or problems trying to use the test helper methods that use mockjax.

@tmcgilchrist
Copy link
Author

I'm not really familiar with how package management works with node/js stuff and whether you can just get it to install mockjax as a dependency of this package. That's certainly how I'd expect it to work from ruby.

But it is confusing to be able to use ember-data-factory-guy without needing mockjax then using a new part of the api and suddenly things don't work (cause you're missing mockjax).

@danielspaniel
Copy link
Collaborator

Agree .. I was trying to be too minimalist and not include something if some did not use it, but it then backfires if you actually use it. Will include mockjax fom now on to avoid that confusion.

@danielspaniel
Copy link
Collaborator

mockjax is now included in the ember-data-factory-guy dist file.
You don't have to do anything anymore, it's just there for you to use when you use this library.

This is as of version 0.7.6, which also has a new method handleFindMany in the FactoryGuyTestMixin class, which does kind of exactly what I just showed you above, but what the heck, it's a one liner instead of 2 now.

@tmcgilchrist
Copy link
Author

Thanks I'll get this updated ASAP.

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