question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing engine service during acceptance test

See original GitHub issue

We are running an acceptance test, during which we want to stub out a method on one of the services which is included on the engine. I don’t think/I can’t find a clean way to get access to this service from the acceptance test.

Originally we tried this:

test('send view event on result page', async function(assert) {
  let count = 0;
  this.application.resolveRegistration('booking-flow@service:ecommerce-analytics').reopen({
    reportView(folder) {
      assert.ok(folder.get('cents'), 'Folder has a price');
      assert.ok(folder.get('origin'), 'Folder has an origin');
      assert.ok(folder.get('destination'), 'Folder has a destination');
      count++;
    },
  });

  /* some function that gets test to right state */
  await goToResult.call(this, assert);

  assert.equal(count, 1, 'reportView is called once');
});

The problem being here that we reopen the class and not the instance of the service, so 1 test works, subsequent tests break as they have all the other stubs on them. We solved this by doing the following:

test('send view event on result page', async function(assert) {

  let count = 0;
  reopenEcommerceAnalytics(this, {
    reportView(folder) {
      assert.ok(folder.get('cents'), 'Folder has a price');
      assert.ok(folder.get('origin'), 'Folder has an origin');
      assert.ok(folder.get('destination'), 'Folder has a destination');
      count++;
    },
  });

  await goToResult.call(this, assert);

  assert.equal(count, 1, 'reportView is called once');
});

async function reopenEcommerceAnalytics(context, hash) {
  // we need to access the app so the router is instantiated and can be looked
  // up below via lookup('router:main')
  await visit('/');

  let router = getOwner(context).lookup('router:main');

  // object hash wich contains all instances of this engine, where the key is
  // incremented for every new instance of this engine
  let bookingFlowInstances = router._engineInstances['booking-flow'];

  // since we only mounted the engine once, the current booking flow engine
  // instance is always via the first (and only) key
  let [instanceKey] = Object.keys(bookingFlowInstances);
  let bookingFlowEngine = bookingFlowInstances[instanceKey];

  let ecommerceService = bookingFlowEngine.__container__.lookup('service:ecommerce-analytics');

  ecommerceService.reopen(hash);
}

Is there an easier way to get an instance of a service registered on an engine from a host app? Or a better way to test a service on an engine?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jbanduracommented, Mar 25, 2019

are there any news about making this work when using new test syntax?

0reactions
Leooocommented, Jan 7, 2021

@lougreenwood would that help you?

//extend-service.js
import {getContext} from '@ember/test-helpers';
import {run} from '@ember/runloop';
import {dasherize} from '@ember/string';

export function extendService(name, serviceStub) {
  const {owner} = getContext();
  //can add separate mapping later on if the list grows
  const addonName = name === 'metrics' ? 'ember-metrics' : 'ukh-web-addon-parent-dependencies';
  const service1 = require(`${addonName}/services/${dasherize(name)}`).default;
  const service2 = service1.extend(serviceStub);
  run(() => {
    owner.unregister(`service:${name}`);
    owner.register(`service:${name}`, service2);
  });
}

and use it in a beforeEach hook:

const windowServiceStub = {};
extendService('window', windowServiceStub);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Factory Acceptance Testing - What Is FAT, and How Does It ...
A Factory Acceptance Test is a test that runs on the equipment or components before it is delivered to its intended destination. While...
Read more >
What is User Acceptance Testing (UAT)? Examples - Guru99
1. One of the most important activities in the UAT is to identify and develop test scenarios. These test scenarios are derived from...
Read more >
NFPA 110 Acceptance Testing - Curtis Power Solutions
NFPA 110 requires that emergency power supply systems undergo acceptance testing to confirm that the system will perform as required.
Read more >
User Acceptance Testing Tools and Checklist | Quick Guide
User Acceptance Testing (UAT) tools, principles, working, and best practices for testing software systems to accept user requirements.
Read more >
Site Integration Acceptance Test Procedures
III Scope of Work and tested in the San Gabriel Valley Pilot Project Acceptance ... Provides various network services that support the IEN....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found