Skip to content
This repository has been archived by the owner on May 26, 2019. It is now read-only.

Improve integration test helpers documentation #562

Closed
eccegordo opened this issue Aug 11, 2015 · 7 comments
Closed

Improve integration test helpers documentation #562

eccegordo opened this issue Aug 11, 2015 · 7 comments

Comments

@eccegordo
Copy link

It seems that there needs be a section on "Integration Test Helpers". That discuss how to build a custom helper for integration testing of components.

For example:

Imagine I have a useful but repetitive little bit of code throughout my component tests

var wsREGEX = /[\s\n]+/g;
this.render(hbs`{{my-component}}`);
assert.equal(this.$().text().trim().replace(wsREGEX, ' '), 'text after whitespace trimming');

What is not clearly documented is how I could create and use a helper in my integration tests.

I would like to do something like this

import Ember from 'ember';

export default Ember.Test.registerAsyncHelper('innerTextFor', function(app, content) {
  var wsREGEX = /[\s\n]+/g;
  return content.replace(wsREGEX, ' ');
});
this.render(hbs`{{my-component}}`);
assert.equal(innerTextFor(this.$().text().trim()), 'text after whitespace trimming');

The goal being to DRY up test logic. The current guides have test helpers and unit test helpers but no integration test helpers.

I believe the above doesn't work for me because of the startApp stuff doesn't seem to be available when in integration mode for tests.

That being said a lot more love could be given to test story overall. In Ember testing is still far more painful than it should be. Need MOAR examples!

@michaelrkn michaelrkn changed the title Guides need to improve integration test helpers documentation Improve integration test helpers documentation Aug 11, 2015
@jimbeaudoin
Copy link

This can be related to #501

@eccegordo
Copy link
Author

One other thing that is not clear from the guides is how to stub out services as part of integration tests.

In my particular case I have some service objects that are injected into specific components. But the components tests fail because the service object is not properly setup and torn down. It would be nice to know how to mock these services with a test helper that works with component integration mode tests.

Faking out a specific API endpoint for example.

@mattxyzeth
Copy link

I'm also having an issue trying to stub a service that's being injected in a component. I have a couple ideas of how to do this, but they all seem hacky. Would be nice to have some clarification on how to achieve stubbing services.

@lukesargeant
Copy link

The easiest work around/fix is to use Ember.inject.service in your component and remove the initializer.

@michaelrkn
Copy link
Contributor

I'm going to close this out for now - I'm not totally sure whether this belongs in the Guides, API docs, or a blog post/etc outside of official Ember stuff, but it doesn't seem like a high priority. I'm totally happy to review a pull request if somebody wants to put something together, although it might be best to start it as a blog post and then open a PR.

@toddjordan you've been super helpful with reviewing the existing testing materials - do you have any thoughts on this, or on any other content you think is missing from the Testing section?

@toddjordan
Copy link
Contributor

@michaelrkn
For the issue of helpers for integration tests, it should be the same way you do it for unit tests. You don't get the acceptance test helpers in component integration tests. Therefore I believe creating a helper is as easy as dropping your helper file in the tests/helpers directory and just importing it into your tests. The helper would be a simple object or function with the logic you want to reuse. There's no need for registration like there is for acceptance tests. We can add a small example for the unit test basics section on this if its something folks would find helpful.

For the part on stubbing services in the comments above, I actually have a branch on my fork (toddjordan@0730c86) that I was planning PR onto component testing as part of its own issue. The thing I need clarification for is I'm not sure if the way I'm registering and looking up stub services is using things that are public an sanctioned. I can open an issue for stubbing services and PR my change and try to get feedback from some of our core team.

@michaelrkn
Copy link
Contributor

@toddjordan 👍 on both counts!

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

No branches or pull requests

7 participants