Lorraine Bernard
Lorraine Bernard

Reputation: 13420

How to convert a fake server from Sinon to Jasmine.

I found an example how to create a fake server using Sinon.
Here is the code (1), (2).

It will be possible to make the same thing using just Jasmine?
If yes. How should I rewrite the code (1) and (2) ?


(1)

        beforeEach(function () {
            this.server = sinon.fakeServer.create();
            this.server.respondWith(
                'GET',
                Routing.generate('api_get_url') + '/' + this.model.get('id'),
                JSON.stringify(this.fixtureResponse)
            );
        });

(2)

        it('should the response not change', function() {
            this.model.fetch();
            this.server.respond();
            expect(this.fixtureResponse).toEqual(this.model.attributes);
        });

Upvotes: 1

Views: 1529

Answers (1)

lambshaanxy
lambshaanxy

Reputation: 23062

Depends on how your code is accessing the server, but if it's using jQuery's $.ajax or $.get (or something similarly centralized) the way Backbone does, you can stub that out and return fake responses instead. So #1 would look roughly like this, in CoffeeScript:

spyOn($,'get').andCallFake (options) =>
  if options.url == Routing.generate('api_get_url') + '/' + @model.get('id')
    options.success(JSON.stringify @fixtureResponse)

See also: Preventing AJAX call with Jasmine and Sinon using Backbone

Upvotes: 1

Related Questions