Stubbing Out
Unlike Salesforce's testing environment, Fonteva allows you to simulate behavior by stubbing out all aspects of Salesforce. For example:
Fonteva stubs out the helper in runBeforeEach.
EventRegistrationFlowDetailsControllerSpec.js
this.sandbox.stub(helper)
You can take it further and, for example, call doInit inside of the helper and provide your own return value.
EventRegistrationFlowDetailsControllerSpec.js
this.sandbox.stub(helper,'doInit',),returns({'adsf'}));
Then we remove that stub after each test in runAfterEach in order to start clean on each run.
EventRegistrationFlowDetailsControllerSpec.js
this.sandbox.restore()
After doing a runBefore or a runBeforeEach, it's advisable to clean out your stubs so you don't impact later tests. It's possible for stubs to remain in global variables causing incorrect results on subsequent tests.
In sinon (http://sinonjs.org/), you cannot stub out the same method multiple times.
You can stub out the .document (line 62) and then delete it (line 75).
EventRegistrationFlowDetailsControllerSpec.js
@runBeforeEach
beforeEach() {
this.sandbox.stub(helper)
global.document = {
title: '',
body: {
classList: {
add: this.sandbox.spy()
}
}
};
}
@runAfterEach
afterEach() {
this.sandbox.restore()
delete global.document;
}
You can individually stub out auraFactory (line 49) so every time something receives this event, it gets the dummy event instead of the real event.
EventRegistrationFlowDetailsControllerSpec.js
@runBefore
before() {
global.$A = auraFactory({
'e.c:EventCancelRegistrationEvent': eventFactory()
});
this.sandbox = sinon.sandbox.create();
}
@runAfter
after() {
global.$A = auraFactory()
}
Previous: Example Controller File | Next: Framework Adapters