Instructor: Here we have a test that stubs out a get call to the API/to-dos endpoint and responds with some test data. We'll do this in a lot of our tests, and having all this data scattered around our test will make our test messy after a while.
Let's see how we can move this data into a fixture file and use it from there. In the Cypress directory, we have another directory called Fixtures.
I'm going to add a new file to that Fixtures directory. I'm going to call it, "todos.json." Now, I'm going to grab that sample data from the test. I'll copy that and paste it into todos.json.
Now, I'm going to go back to the spec file. I'm going to delete this array. Above our call to cc.rail, I'm going to add a call to cy.fixture. I'm going to pass fixture the name of our JSON file.
I'm going to tack a then onto it. Here we're going to get our result. I'll move cy.rail into the then. I'll pass to-dos as the data. I'll save this.
We'll double-check that our test is running and everything's working just fine. This has cleaned up our test quite a bit. We've moved all that data out into a fixture file.
We can easily re-use it in other spec files. This is such a common setup that we can actually shorten this and make it even cleaner. Rather than calling cy.fixture and then using our then, we can call cy.rail.
We can replace this data with a string that uses a fixture prefix followed by the name of our fixture file.
Now we're back down to one line. We've cleaned up all the test data. If I save this and switch back to the Cypress runner, we'll see that our test is still running as it was before.
Just to verify that this is using our fixture file, let's open our to-dos. We'll update one of the items. We'll save it.
We'll re-run our test, and we'll see that that is in fact the data being loaded from our fixture file.