Fixing existing tests

First things first; we must make our existing tests pass. In the speakerActions.spec.js file, find the first test that we skipped and remove the skip. This will cause that test to fail with:

Cannot read property 'then' of undefined

Back in the beforeEach method, where we are creating the speaker service, we need to create a new Sinon stub for a service method. Looking at the test, we can see that the first service call we make is to get all speakers. So, let's start there:

beforeEach(() => {
let service = factory.createSpeakerService();
let mockService = new MockSpeakerService();

getAll = sinon.stub(service, "getAll");
getAll.callsFake(mockService.getAll.bind(mockService));

mockStore = configureMockStore(middleware);
});

Looking at this code, what we have done is to create a new Sinon stub and redirect calls to the service getAll method to the mockService getAll method. Lastly, we bind the mockService call to the mockService to preserve access to private variables in the mockService.

Running the tests again, we get a new error:

Attempted to wrap getAll which is already wrapped

What this error is telling us is that we have already created a stub for the method we are trying to stub. At first, this error may not make any sense. But, if you look we are doing this in a beforeEach. Sinon is a singleton and we are running our mocking commands inside a beforeEach, so it already has a getAll stub registered by the time the second test is preparing to run. What we must do is remove that registration before we try to register it again. Another way to say this is that we must remove the registration after each test run. Let’s add an afterEach method and remove the registration there:

afterEach(() => {
getAll.restore();
});

That fixes the first failing test that we had, now to fix the other two. The process will be largely the same, so let's get started.

Remove the skip from the next test. The test fails. We are calling the getSpeaker action in this test and if we look at the speaker actions, we can see that it uses the getById service method. As before we will need to stub this method in the beforeEach.

getById = sinon.stub(service, "getById");
getById.callsFake(mockService.getById.bind(mockService));

As before, we are now getting the already wrapped message:

Attempted to wrap getById which is already wrapped

We can fix this one the same way we fixed the last one, by removing the stub in the afterEach function.

getById.restore();

We are back to all passing tests with one skipped. The last test is the exact same process. Here are the full beforeEach and afterEach functions when we are done:

beforeEach(() => {
let service = factory.createSpeakerService();
let mockService = new MockSpeakerService();

getAll = sinon.stub(service, "getAll");
getAll.callsFake(mockService.getAll.bind(mockService));

getById = sinon.stub(service, "getById");
getById.callsFake(mockService.getById.bind(mockService));

create = sinon.stub(service, "create");
create.callsFake(mockService.create.bind(mockService));

mockStore = configureMockStore(middleware);
});

afterEach(() => {
create.restore();
getAll.restore();
getById.restore();
});

Don't forget to remove the skip from the last test. When all is said and done you should have 42 passing tests and 0 skipped tests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.188.95