I'm currently writing a javascript game module that takes the following parameter as constructor argument:
{
gameId : "string for unique game id",
cssSelector: "selector for target (used to initialze game)"
}
I have found a way to get around this, see new comment below
I have a pretty good test coverage on everything else, but I can't figure out how to write the following Jasmine test:
describe("Initialization of game", function() {
it("Should throw expection if css selector is not found", function() {
// what goes here?
//negative scenario
expect(function(){
var game = new Game({gameId : '1', cssSelector : "#not-found"});
}).toThrow("Cannot find the element corresponding to cssSelector");
//positive senario
expect(function(){
var game = new Game({gameId : '1', cssSelector : "#found"});
}).not.toThrow("Cannot find the element corresponding to cssSelector");
});
"Solution" I say "solution", because it feels a bit like a hack to get around this. I use the fact that the test is run in HTML and that I can manipulate the environment. So what I did was:
- In the negative scenario, use a specifier that is not found. The first expectation will then not fail.
- In between on the positive and negative test case, I used jQuerys .append() method to add a div with id "found" to the body
That's it!