Lorraine Bernard
Lorraine Bernard

Reputation: 13420

Get fixture data using sinon.fakeServer

I am trying to simulate Backbone.Model.fetch request using sinon.fakeServer or sinon.useFakeXMLHttpRequest and require.js.

Here's my piace of code which is not working properly (1)

My question is:
how can I get the fixture data using sinon.fakeServer?
Please the two comments at the end of this piece of code.

P.S.:
If I make the fetch request commenting the code about sinon.fakeServer, it makes the get request to the server.
If I make the get request using sinon.fakeServer it does not fetch anything (both server and fixture)


(1)

define([
    'js/models/myModel',
    'js/spec/fixtures/myModel.fixture'
], function (MyModel) {

            beforeEach(function () {

        this.myModel = new MyModel();

                console.log("fixtures", this.fixtures.Task, this);
                this.fixture = this.fixtures.Task.valid;
                this.fixtureTask = this.fixture.response;
                this.server = sinon.fakeServer.create();
                this.server.respondWith(
                    "GET",
                    Routing.generate("api_get_tasks"),
                    JSON.stringify(this.fixture)
                );
            });

            afterEach(function () {
                this.server.restore();
            });


            it("should make the correct request", function() {
                this.server.respond();

                this.feeds.fetch();

            console.log(this.fixture); // this response is OK
            console.log(this.myModel.attributes); // it does not take the value from this.fixture

            console.log("fixtures", this.fixtures.Task, this); // see the picture below

             });

});

enter image description here

Upvotes: 2

Views: 1664

Answers (1)

theotheo
theotheo

Reputation: 2702

You don't call the fetch method on the model.

Try this:

 it("should make the correct request", function() {
        this.myModel.fetch();
        this.server.respond();
        console.log(this.fixture); // this response is OK
        console.log(this.myModel.attributes); // it does not take the value from this.fixture

        console.log("fixtures", this.fixtures.Task, this); // fixtures  Object  jasmine.Spec

 });

Upvotes: 1

Related Questions