{"id":214,"date":"2015-11-04T16:15:59","date_gmt":"2015-11-04T07:15:59","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/11\/04\/sinon"},"modified":"2022-10-28T21:59:22","modified_gmt":"2022-10-28T12:59:22","slug":"sinon","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/11\/04\/sinon\/","title":{"rendered":"karma\u306bsinon.js\u3044\u308c\u3066\u3001fakeServer\u3092\u3064\u304f\u308b"},"content":{"rendered":"

\u6982\u8981<\/strong><\/h2>\n

SPA\u306e\u30c6\u30b9\u30c8\u7528\u306bAPI\u30b5\u30fc\u30d0\u30fc\u3092\u30e2\u30c3\u30af\u3057\u305f\u3044\u304c\u3001nock<\/a>\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u30b7\u30f3\u30d7\u30eb\u306b\u5b9f\u88c5\u3057\u305f\u3044(nock\u306f\u30d6\u30e9\u30a6\u30b6\u74b0\u5883\u3067\u3042\u308bkarma\u3067\u306f\u52d5\u304b\u306a\u3044)\u3002<\/p>\n

mocha\u3092\u4f7f\u3046\u306e\u3082\u5197\u9577\u306a\u306e\u3067\u3001sinonjs<\/a>\u3092\u4f7f\u3046\u3053\u3068\u306b\u3057\u305f\u3002<\/p>\n

\u5b9f\u88c5<\/strong><\/h2>\n

karma\u306econfig\u306bsinon\u3092\u52a0\u3048\u308b\u3002\u3053\u308c\u3067\u3001\u30c6\u30b9\u30c8\u5185\u3067sinon\u304c\u30b0\u30ed\u30fc\u30d0\u30eb\u3067\u4f7f\u3048\u308b\u3002<\/p>\n

\/\/ karma.conf.js\n\n...,\nframeworks: ['jasmine', 'sinon'],\n...,\nplugins: [\n  ...,\n  require('karma-sinon'),\n],\n...,\n<\/code><\/pre>\n

\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3044\u305f\u3002\u2460\u304b\u3089\u2465\u306e\u9806\u756a\u3067\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u308b\u3002<\/p>\n

\u2462\u2192\u2463\u2192\u2464\u2192\u2465\u3068\u51e6\u7406\u304c\u8d70\u308b\u306e\u304c\u30dd\u30a4\u30f3\u30c8\u3067\u3001server.respond();<\/code>\u304casync\u95a2\u6570\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3057\u3001\u2464\u3067\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u3001\u2465\u3067jasmine\u306e\u975e\u540c\u671f\u30c6\u30b9\u30c8\u7d42\u4e86\u95a2\u6570done<\/code>\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3002<\/p>\n

\/\/ test.js\n\nimport request from 'superagent';\n\ndescribe('sinonjs', function() {\n  let server = null;\n\n  beforeEach(() => {\n    \/\/ \u2460\u7591\u4f3c\u30b5\u30fc\u30d0\u306e\u751f\u6210\n    server = sinon.fakeServer.create();\n  });\n\n  it('fakeServer', (done) => {\n\n    \/\/ \u2461\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u6307\u5b9a\n    server.respondWith('GET', '\/some\/endpoint', [\n      200, { 'Content-Type': 'application\/json' }, 'res',\n    ]);\n\n    \/\/ \u2462\u30ea\u30af\u30a8\u30b9\u30c8\n    request\n      .get('\/some\/endpoint')\n      .end(function(err, res) {\n        expect(res).to.be('res'); \/\/ \u2464\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\n        done(); \/\/ \u2465\u975e\u540c\u671f\u30c6\u30b9\u30c8\u306e\u7d42\u4e86\n      });\n\n    \/\/ \u2463\u7591\u4f3c\u30b5\u30fc\u30d0\u304b\u3089\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\n    server.respond();\n  });\n\n  afterEach(() => {\n    \/\/ \u2466\u7591\u4f3c\u30b5\u30fc\u30d0\u3092\u7834\u68c4\n    server.restore();\n    server = null;\n  });\n});<\/code><\/pre>\n

\u53c2\u8003<\/h2>\n