Let's say I have this function I want to test:
var test = function () {
console.log('words!');
};
I'd write something like this
define('test()', function () {
it('prints "words!" to the screen', function() {
test();
expect(<browser logs>).toContain('words!'); // TODO
}
}
But I don't know how to view the console logs or if this is even possible. Preferably, I'd do this in any browser, or at least PhantomJS.
You may create the spy on console.log function. The code may look like ...
describe("log reporting", function () {
beforeEach(function(){
spyOn(window.console, 'log');
});
it('should print log message to console', function(){
test();
expect(window.console.log).toHaveBeenCalled();
})
});
With this example you would know your console.log function was called. This is exactly what you need to know. You don't want to compare logged message with expected value, simply because you would unit test not your code, but window.console.log function itself, which you didn't write ;) You may call ".and.callFake(function(){do something});". In this case you would do something instead of actual console.log call, for example check your value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With