2

I have a problem when I'm trying to test some javascript, I'm not sure that the code is actually testable, but then I would be pleased to know how to make it testable.

$("#Button").on('touchstart', () => testFunc());

function testFunc() {
    button(true, userInfo, gameCode);
}

I want to test when the button is touched that the testFunc is called. I'm using Jasmine as my test framework.

But for whatever reason when I try to call the testFunc from Jasmine just to test that something is working I get the error

"ReferenceError: Can't find variable: testClass in file:///E:/Dokumenter/SemesterProjekt%20fun%20stuff/PRJ4Web/TankWebApplication/TankWebApplication/Test/Test.js (line 8)s"

I have made the reference to the file. So I'm not sure what is going on.

The test code that gives me the error

describe("Joystick test for functionallity",
function() {
    beforeEach(function() {

    });


    it("All default values",
        function() {
            testFunc();
        });
});

How do I test this code? Is it possible to do?

0

1 Answer 1

2

Your goal is to see if the function will be called upon button click, therefore, you must check the actual function call with toHaveBeenCalled jasmine method.

Invoking the functions by itself doesn't make any sense (like in your example)

You should do something like this:

  it('All default values', => {

    spyOn(class.testFunc);
    document.getElementById('Button').click();

    expect(class.testFunc).toHaveBeenCalled();
  }));
 })
Sign up to request clarification or add additional context in comments.

4 Comments

I still get the error that I talk about in the post, do you know what that can be?
is testClass a global variable in your namespace class?
I'm pretty sure it is a global variable.
You should set up your test environment better. Basically our your test.js file can not see your global variable and is complaining. You haven't even gotten to the button click event check because of that error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.