"A good developer will thoroughly unit test his code, while a lazy good developer will automate his unit tests"
"cds": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind",
"--credentials": {
"destination": "NorthWind"
}
}
}
}
"cds": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind"
}
},
"[local]": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind",
"credentials": {
"url": "https://services.odata.org/Experimental/OData/OData.svc"
}
}
}
},
"[production]": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind",
"credentials": {
"destination": "NorthWind"
}
}
}
}
}
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind"
}
},
> cds watch
"[local]": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind",
"credentials": {
"url": "https://services.odata.org/Experimental/OData/OData.svc"
}
}
}
},
> NODE_ENV=local cds watch
"[production]": {
"requires": {
"NorthWind": {
"kind": "odata",
"model": "srv/external/NorthWind",
"credentials": {
"destination": "NorthWind"
}
}
}
}
For some reasons, the configuration above doesn't work if you test locally using the command below. But if you deploy the application to SCP Cloud Foundry, the application will work without any issues. Also, if you use this credentials without the [production] profile, the credentials.destination = NorthWind works locally. This is already taking into account that you have properly setup the credentials in the file default-env.json. |
Update on 2 June 2020: The issue of using [production] profile for local testing is really a bug in the framework -- see below issue: https://github.com/sapmentors/cap-community/issues/67 Thanks to wattnu for confirming that this issue exists! |
> NODE_ENV=production cds watch
> npm install --save-dev mocha chai chai-http
"scripts": {
"test": "mocha tests/test.js --timeout 15000 --exit",
"start": "npx cds run"
},
const cds = require("@sap/cds");
cds.exec("run", "--with-mocks", "--in-memory?");
module.exports = new Promise((resolve) => {
cds.on('listening', () => {
resolve(cds.app);
});
});
As you can see from the bootstrap code, we are running cds programmatically rather than running it from a command line using cds watch. In fact, this is the code/function being called whenever we execute the cds watch command. Note that running the application using mock data for unit testing ensures that our test cases will work correctly. When writing test scripts, you evaluate the data provided by the service with the expectation of what the value is supposed to be. Using mock data, then you can always be sure that the data returned by the application is consistent with the configured mock data. |
const chai = require("chai");
const chaiHttp = require("chai-http");
const server = require("./server");
// Configure chai
chai.use(chaiHttp);
chai.should();
let app = null;
before((done) => {
server.then((result) => {
app = result;
done();
});
});
describe("Products Operation", () => {
describe("GET /catalog/Products", () => {
it("+ should return a list of products", (done) => {
chai.request(app)
.get("/catalog/Products")
.end((error, response) => {
try {
response.should.have.status(200);
response.body.value.should.be.an("array").to.have.lengthOf(2);
done();
} catch (error) {
done(error);
}
});
});
});
});
> npm run test
Note that we only have one test script in this case because the intention of this blog is to demonstrate on how to setup Mocha in the CAP Model context. To know more about the Mocha test framework, I suggest that you browse through the official website for documentation and API reference -- https://mochajs.org/. |
// This is just a demostration of a failed test case
// The assertion was deliberately changed to trigger the test to fail
it("- should return a list of products", (done) => {
chai.request(app)
.get("/catalog/Products")
.end((error, response) => {
try {
response.should.have.status(200);
response.body.value.should.be.an("array").to.have.lengthOf(12);
done();
} catch (error) {
done(error);
}
});
});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 |