famous-dog-82296
05/23/2023, 9:52 AMfamous-dog-82296
05/24/2023, 1:58 PM[ "table", "of", "items" ].forEach((item)=>{
it(`Item: ${item}`, ()=>{
expect(item).to.be.a("string");
})
})
I'm not a fan of the idea that I should have a fixture/table because the data is mutable, it doesn't feel right. I want to use data from within my application. So the best way felt like creating larger and larger single tests to cover both which also doesn't feel right.famous-dog-82296
05/24/2023, 2:02 PM/api/creatives and /api/creative/${uuid} which I'm testing. Right now using a staging database to make sure all the basics are there, at a later stage I want to build up mocking and testing from a blank database upwards. Here's a sample of what I mean.
js
describe("GET: api/creatives/", () => {
beforeEach(() => {
cy.fixture('defaultUserDetails').then((userDetails)=>{
cy.session_login(userDetails.name, userDetails.user, userDetails.pass)
})
})
it(`should return all non archived data`, () => {
//cy.fixture('creatives_temp').then((response) => {
cy.request("/api/creatives").then((response) => {
expect(response.status).to.eq(200);
expect(response.headers["content-type"]).to.eq("application/json");
expect(response.body).to.be.a("array");
return response.body
}).then((data) => {
data.forEach((element) => {
cy.request(`/api/creative/${element.uuid}`).then((response)=>{
expect(response.status).to.eq(200);
expect(response.headers["content-type"]).to.eq("application/json");
expect(response.body).to.be.a("array");
return response.body
}).then((specific)=>{
expect(element.id).to.be.a("number").be.finite.and.eq(specific.id);
expect(element.name).to.be.a("string").that.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i).and.eq(specific.name);
expect(element.uuid).to.be.a("string").that.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i).and.eq(specific.uuid);
})
})
})
})
})
This just doesn't feel like the right way to do itgray-kilobyte-89541
05/24/2023, 2:04 PMfamous-dog-82296
05/24/2023, 2:04 PMgray-kilobyte-89541
05/24/2023, 2:05 PMfamous-dog-82296
05/24/2023, 2:05 PMfamous-dog-82296
05/24/2023, 2:06 PMfamous-dog-82296
05/24/2023, 2:07 PMgray-kilobyte-89541
05/24/2023, 2:07 PMfamous-dog-82296
05/24/2023, 2:09 PMgray-kilobyte-89541
05/24/2023, 2:10 PMfamous-dog-82296
05/24/2023, 2:11 PMfamous-dog-82296
05/24/2023, 2:13 PMfamous-dog-82296
05/24/2023, 2:16 PMbefore(()=>{
cy.fixture('creatives_temp').then((response) => {
expect(response.status).to.eq(200);
expect(response.headers["content-type"]).to.eq("application/json");
expect(response.body).to.be.a("array");
return response.body
}).as("test_state")
})
it.each(cy.get("@test_state"))("Test %s", (item)=>{
cy.log(item)
})
Was my immediate thoughtfamous-dog-82296
05/24/2023, 2:19 PMgray-kilobyte-89541
05/24/2023, 2:22 PMjs
before(()=>{
cy.fixture('creatives_temp').then((response) => {
expect(response.status).to.eq(200);
expect(response.headers["content-type"]).to.eq("application/json");
expect(response.body).to.be.a("array");
return response.body
}).as("test_state", {keep: true})
})
BUT: it still won't work because you cannot create additional tests when a test starts running (which is when before hook runs). Thus you would need to requests the data for example from your config file and pass it to the spec via env object. I show an example like this in my paid course lesson https://cypress.tips/courses/network-testing/lessons/bonus90famous-dog-82296
05/24/2023, 2:24 PM