When I run my jasmine specs I get the following error:
Error: Expected a spy, but got undefined.
My coffeescript code:
describe "setupForm", ->
beforeEach ->
spyOn(Subscription.prototype, 'runSimulation')
it "calls subscription.runSimulation when form is submitted with number", ->
Subscription.prototype.runSimulation()
expect(Subscription.prototype.runSimulation()).toHaveBeenCalled()
I have simplfied my erroring code to the above for debugging, but I can't figure out why it is saying the spy is never called when I'm explicitly calling it my test. I am testing the method in other places, so I think the error has to be with how I am using the Jasmine Spy. Thanks.
Take the () off the end of Subscription.prototype.runSimulation():
expect(Subscription.prototype.runSimulation).toHaveBeenCalled()
Related
It gives me TypeError: Cannot assign to read only property 'get' of object '#<Config>'.
I'm using mocha 8.0.1, chai 4.2.0, sinon 9.0.2 for Unit Testing.
I'm spying on a method in the config npm package.
Here's how I spy the get method:
...
before(() => {
sandbox = sinon.createSandbox();
configStub = sandbox.spy(config, 'get');
});
after(() => {
sandbox.restore();
});
it('should something', async () => {
console.log('Just logging');
config.get('LOG.LEVEL'); // just to show the point. if I remove this line, it doesn't throw the error
});
...
What happens is, if I run config's get method somewhere during the test, it can not be restored by spy. It throws that read-only property error. But when the config.get function is never called, it doesn't throw that error (I don't understand why not). For stub there's no problem, it can restore just fine.
But the reason I'm using spy is because I want config.get to work like it normally does while I'm testing my module/function that's using it, I just want to spy on it. And I also need to be able to restore it after this test suite. I spy on it because I need to test that it's being called by my module/function with some specific parameters.
How do I spy on a read-only property/method, allow my module/function to use it like it normally does, and then restore it?
Thank you :)
You can set ALLOW_CONFIG_MUTATIONS environment variable to true for the test run.
In this way config.get invocation will not freeze the config which should resolve the issue.
You can find the env var description in the documentation:
https://github.com/lorenwest/node-config/wiki/Environment-Variables#allow_config_mutations
I have a Karma test which looks like this:
it('testing if test will fail on exception', function () {
expect(true).toBe(true);
throw "an exception";
expect(false).toBe(true);
});
The problem is this test returns valid, because the exception is thrown before the second test condition was checked. I need a solution that will work on all tests, so implementing try/catch is not the solution I am searching for. Is there a way to set Karma/Jasmine configs so that all tests fail on exception thrown? Any other good ideas?
I have one Jasmine test that is continously failing due to a spyOn not executing.
The following test will automatically fail:
it('simple test', function() {
spyOn(angular, 'element');
});
The error is:
TypeError: 'undefined' is not an object (evaluating 'angular.element(handle.elem).off')
at /Users/geoff/Project/www/components/angular-mocks/angular-mocks.js:1946
at /Users/geoff/Project/www/components/angular-mocks/angular-mocks.js:1977
This error only seems to happen with angular.element. spying on other angular methods such as angular.copy and angular.forEach do not throw this error. I am using Jasmine 2.0 and Angular ~1.3. Any advice on fixing this problem would be appreciated.
You need to allow access to the real object.
spyOn(angular, 'element').and.callThrough();
The code is trying to access a property on the return value, but the spy is not returning anything. You can't access .off on an undefined object!
Update: I figured out what the issue was, see my comment below.
Is there a way to guarantee state before each Jasmine test?
For example:
describe('some thing', function () {
beforeEach(function () {
doSetup();
// this expect does not evaluate :(
expect(something).toBe(inSomeState);
});
it('has some behavior', function () {
// test code
});
});
The expect inside of the setup make no difference at all. Even throwing an error in the beforeEach does nothing. I would like to have some assurance that the setup has completed correctly before running the next test.. is there a way to do this?
Okay I see what the issue is. Hoping this will help other people out there having this same issue. grunt-contrib-jasmine does not display errors or failed expects inside of beforeEach or afterEach with the display option set to short. Setting this option to full will once again display Errors and failed expects.
I am testing javascript code that throws an exception (temporarily, early TDD state) but jasmine passes the test.
Is there any way to set up jasmine such that it fails with an unexpected exception?
Are there other javascript unit test frameworks that do not pass such tests?
Make sure you are using the latest version 1.2.0.
The code bellows fails:
describe("must fail on error", function(){
it("a + 1 should produce an error", function(){
expect(a + 1).toEqual(2);
})
});
I stumbled on this post searching for something else related to Jasmine testing. Before seeing this question, I saw this a blog post on testing exceptions with Jasmine. In that post, the author uses the bind command like this:
it('should allow us pass in parameters', function () {
expect(myOtherProcedure.bind(null, 10, 'you generated: ')).not.toThrow();
});