Java Reference
In-Depth Information
negative = int*-1;
decimal = Math.floor + 0.5;
});
This is a setup function and it contains any code that will be run before every spec. In this
case we're using it to create three random numbers that are assigned to variables with glob-
al scope. The first, int , is an integer between 1 and 100 . The second, negative , is
the negative of that number, and the last, decimal , is the same number, plus 0.5 . These
numbers can now be used in the specs and will use a different number in each test. Now
add the following specs inside the " The factorsOf() function " describe function:
spec/numberSpec.js (excerpt)
it("should throw an exception for negative numbers",
function() {
expect(function(){ factorsOf(negative) }).toThrow();
});
it("should throw an exception for non-integer numbers",
function() {
expect(function(){ factorsOf(decimal) }).toThrow();
});
These specs use the toThrow() method to check that an exception has been thrown. One
point to note here is that the factorsOf() function being tested needs to be wrapped in
an anonymous function for it to work.
While we're at it, we can add some extra specs in the " The isPrime() function "
describe function that deal with these numbers. No exceptions are necessary in these cases;
negative numbers and non-integers are simply not prime, so the function should return
false :
spec/numberSpec.js (excerpt)
it("should say a negative number is not prime", function()
{
expect(isPrime(negative)).toBe(false);
});
Search WWH ::




Custom Search