javascriptjasminetddfixture

Jasmine : Fixture could not be loaded


So i wanted to get into Test Driven Development and decided to use Jasmine on my project.

The thing is, i can't load fixtures.

The 2 solutions commonly proposed are :

  1. Run chrome with --allow-file-access-from-files
  2. Serve the file from you local server

So i used the first solution, but no result.

Then i set up the routes of my webserver so that localhost/fixture/my_fixture would return the content of my_fixture.html.

So when i manually access localhost/fixture/my_fixture, the content of the fixture is displayed on screen. But in my jasmine spec file, when i use :

jasmine.getFixtures().fixturesPath = 'http://localhost/fixture'
loadFixtures('quizz_fixture')

I get the following errors :

Error: Fixture could not be loaded: http://localhost/fixture/quizz_fixture
(status: error, message: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost/fixture/quizz_fixture?_=1455854875950'.)

When i use the URL given in the error, my browser displays the content of the fixture without errors.

Therefore, i don't understand the reason for this error. Does anyone have an insight?

Edit:

Edit 2

The issue comes from jasmine-jquery, on line 139 below, where the fail function is called. I can't figure out what's happening as the URL that supposedly can't be loaded actually loads just fine in my browser :

jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
    var self = this
      , url = this.makeFixtureUrl_(relativeUrl)
      , htmlText = ''
      , request = $.ajax({
        async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
        cache: false,
        url: url,
        dataType: 'html',
        success: function (data, status, $xhr) {
          htmlText = $xhr.responseText
        }
      }).fail(function ($xhr, status, err) {
          throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + err.message + ')')
      })

The result is :

Failed to load 'http://localhost/fixture/quizz_fixture.html?_=1456886216017'

Which works when called in the browser. I just don't get it.

Thanks.


Solution

  • So i found a very unsatisfying solution, but a solution nonetheless.

    To summarize

    1. Using chrome, i tried to load jasmine fixture from a local file, which wouldn't work with chrome (this is something known, disabled for security reasons).

    2. I tried using the chrome flag --allow-file-access-from-files but it didn't work. So i gave up on using a fixture from a local file.

    3. I understood that the fixture file had to be served from my web server, which i did. But it didn't work either, because of some Ajax error related to the caching of fixtures. I tried updating my version of jquery (which was a bit old) but it didn't work. In the end, I wasn't able to understand what the issue was.

    4. I downloaded firefox and tried executing the jasmine specRunner with the configuration of point 3 above (fixture served by web server) but again, it didn't work.

    5. Using firefox, I reverted to the method in point 1, which is using a local fixture file, and it did work. I hate that solution, but i need to go forward, so that will do.

    Conclusion

    If stuck with that kind of issue, save yourself some time and use firefox which will allow the use of a local fixture file.