8

There are ton of questions asking how to mock http responses in protractor tests. How to do this is not the question, should we do this is the question.

http://en.wikipedia.org/wiki/Test_fixture#Software

I've been a QA Engineer for over 4 years, and most of my automated test experience deals with both low level (unit) tests of controllers, models, etc and high level (integration) tests of full systems. In my ruby world experience, we used Capybara for integration tests along with blueprint and factorygirl (for different projects) to create mock database entries. This was our integration/E2E testing.

I've only recently moved to a javascript team using AngularJS. The original built-in testing framework (now deprecated) had a mock Backend module which seemed suitable for our needs. Protractor is now the standard. Only after protractor gained steamed, have I heard the backlash of using fixtures for E2E testing. Many posts are pointing out that E2E testing should be testing the full stack, so any backends should not be mocked and be accessible.

Should integration tests use fixtures, and why?

jonh
  • 242
  • 2
  • 10
  • I use both kinds of tests. "E2E" tests with a mocked backend to test details of the UI (does div A appear when I click button B, etc.), and more coarse-grained E2E tests with a real backend to make sure that the communication between the frontend and the backend works as it should. – JB Nizet Dec 23 '13 at 21:53
  • 1
    I'm in the mindset that you should be able to do E2E testing without a backend - since the backend may be developed on a different timeline than the frontend. Mocks can be a useful tool for pressing forward with development and the tests written can easily be changed to accept the actual backend once the mocks are no longer needed. – jonh Dec 23 '13 at 22:57
  • Why concern without using fixtures is when I'm running E2E tests for Create actions multiple times, I will be creating more and more dummy data in my DB and clearing that out will involve another step in my process. – user12121234 Dec 22 '14 at 23:17

4 Answers4

2

There is a vocabulary problem here. What is called "e2e" testing in the Angular world has nothing to do with end-to-end testing. It is an end-to-end of the UI part only, which means no e2e test at all. It is UI testing.

Gojko Adzic, in "spec by example" book, recommands to do functional, fixture-based testing "below the skin of the application", i.e. without the UI part.

To answer your question :

-Should UI tests have fixture? No, use mocks or stubs

-Should Backend tests have fixture ? Yes

bdavidxyz
  • 2,492
  • 1
  • 20
  • 40
  • Can you elaborate this please "Should UI tests have fixture? No, use mocks or stubs"? Thanks – Mladen Janjetovic Dec 29 '16 at 16:25
  • I mean by fixture not only the setup to have the test to work properly, but also the business-related use case that comes along with it. It sometimes makes sense when working on UI, but you can't test all business cases only based on what UI can achieved. – bdavidxyz Dec 30 '16 at 21:28
1

You are asking 2 questions - about the e2e tests and the integration tests. :)

The e2e test, at least in Angular's world, is testing your complete application as a real user can interact with it. This includes testing your backend request and response. However, if that runs slow and requires resources, it makes perfect sense to switch to a smaller (or even fake) version of your backend for testing.

The integration test is about a part of your code, and unit test is about individual units. Both times some or all dependencies can be mocked to isolate the tests.

So in all cases using fixtures or mocks can be useful.

See my answer here for more detailed discussion of use cases, advantages and limitations of Karma and Protractor.

Community
  • 1
  • 1
Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
0

Yes we use ngMockE2E to mock the backend we then expose some helpers to window object so we can seed various mock data states. We also use sinon to force a specific time for testing date sensative UI so all new Date() calls returns what you want

user1750709
  • 563
  • 3
  • 4
  • 2
    Have you guys moved to protractor yet? It doesn't work nicely with the mocked backend provided by ngMockE2E - I know some people have gotten it to work - but I haven't. – jonh Jan 08 '14 at 18:04
0

I'm facing the same issue here with a personal code project. I'm using the MEAN stack, and my solution will be to:

  1. use Grunt to run the tests.
  2. before starting the Node server, use the mongoose fixtures to set up the Mongodb test db (https://github.com/powmedia/mongoose-fixtures)
  3. start the Node server with a parameter to make it use the test db.

You could do something like this approach if on a different stack, although Grunt is very helpful as a general job runner.

ben.tiberius.avery
  • 2,194
  • 1
  • 13
  • 8