title: Jasmine: test that image exists in the folder
body: <p>I have an angularJS project which is tested with Jasmine.
  I need to write a unit test which will check that the image exists in the folder and was not deleted. In my case that's just a default profile picture, for example.</p>
  
  <p>The idea is that we download image and send success on load or not success on error. If I try it in plain JS - everything works like a charm. But somehow it always fails with Jasmine. What's wrong? And is there any better way to test that the image exists and loads correctly?</p>
  
  <p>Sorry for Coffeescript instead of JS. But it still should be easy to follow:</p>
  
  <pre><code>describe "Default services: ", -&gt;
  
      beforeEach angular.mock.module("default.services")
  
      describe "App Image: ", -&gt;
  
          async = new AsyncSpec(this)
          success = false
  
          async.beforeEach (done) -&gt;
              inject (defaultStuff) -&gt;
                  img = new Image()
  
                  img.onload = -&gt;
                      success = true
                      done()
                  img.onerror = -&gt;
                      success = false
                      done()
  
                  img.src = defaultStuff.appImage
  
          it "should have default app image", inject (defaultStuff) -&gt;
            expect(defaultStuff.appImage).not.toBe(null)
  
          async.it "should download default app image (e. g. check that it exists)", (done) -&gt;
              expect(success).toBe(true)
  </code></pre>