views:

63

answers:

2

Here's the problem, I'm trying to do an automatic test to see if addToCart() works correctly.

addToCart();
if(Kiosk.GetCart().toString() !== emptyCartString){
    setTestResults('addToCart', 'Passed');
}
else{
    setTestResults('addToCart', 'Failed');
}

I'm adding a product to an empty cart, then I want to check to see if the cart is still empty, if something is there then addToCart() worked and the test has passed. If the cart is still empty after the addToCart() call then the test should fail.

What is happening is that the addToCart() method takes a while to execute and in the meantime the 'if' statement fires and checks the cart before addToCart() has a chance to return, and thus the test always fails.

Callbacks are the answer, correct? But what's the best way to implement them here?

+1  A: 

Well, I maybe don't quite understand the question, but what's wrong with passing the callback to the addToCart function and having that function call the callback when it's done?

Like this:

addToCart( function() {
    if(Kiosk.GetCart().toString() !== emptyCartString){
        setTestResults('addToCart', 'Passed');
    }
    else{
        setTestResults('addToCart', 'Failed');
    }
} );
Fyodor Soikin
I think this is wrong
Bang Dao
Why exactly do you think this approach is wrong?
Fyodor Soikin
This seemed like the most straight-forward approach I've read about, sadly it didn't work.
ThoughtCrhyme
What exactly did you do? Why exactly didn't it work? Where did you receive an error and what error was that?
Fyodor Soikin
A: 

As I think, function addToCard will post / get to server using ajax. Then add the code

if(Kiosk.GetCart().toString() !== emptyCartString){
    setTestResults('addToCart', 'Passed');
}
else{
    setTestResults('addToCart', 'Failed');
}

To your ajax callback function.

Bang Dao
Unfortunately this didn't work either. The test continues to fail, but when I check the contents of the cart manually there is something there, so I believe it's still a timing issue.
ThoughtCrhyme