如何将假服务器从Sinon转换为Jasmine

How to convert a fake server from Sinon to Jasmine.

本文关键字:Sinon 转换 Jasmine 服务器      更新时间:2023-10-29

我发现了一个如何使用Sinon创建虚假服务器的示例
这是代码(1),(2)。

只用茉莉花就能做出同样的东西吗
如果是。我应该如何重写代码(1)和(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);
        });

这取决于代码访问服务器的方式,但如果它像Backbone那样使用jQuery的$.ajax$.get(或类似的集中式),则可以将其截断并返回假响应。因此,在CoffeeScript:中,#1大致如下

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

另请参阅:防止Jasmine和Sinon使用Backbone 进行AJAX调用