{"id":218,"date":"2015-11-16T09:00:00","date_gmt":"2015-11-16T00:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/11\/16\/nock-with-mocha"},"modified":"2022-10-28T21:59:11","modified_gmt":"2022-10-28T12:59:11","slug":"nock-with-mocha","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/11\/16\/nock-with-mocha\/","title":{"rendered":"Mocha\u3068Nock\u3067\u30e2\u30c3\u30af\u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u3063\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30c6\u30b9\u30c8"},"content":{"rendered":"

\u6708\u306b1kg\u3072\u3088\u3053\u8c46\u3092\u98df\u3079\u308b\u5c0f\u98fc\u3067\u3059\u3002
\n\u5148\u65e5\u5f53\u30d6\u30ed\u30b0\u3067\u5ca1\u672c\u304csinon.js\u3067XHR\u3092\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u30e2\u30c3\u30af\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u65b9\u6cd5<\/a>\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3057\u305f\u3002
\n\u3061\u3087\u3046\u3069\u79c1\u3082\u5225\u306e\u6848\u4ef6\u3067API\u30b5\u30fc\u30d0\u30fc\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30c6\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u5ca1\u672c\u3068\u306f\u9055\u3063\u305f\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u63a1\u7528\u3057\u307e\u3057\u305f\u3002
\n\u672c\u7a3f\u3067\u306fNode.js<\/code>\u306ehttp<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u304b\u3089\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u30e2\u30c3\u30af\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059nock<\/code>\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n

nock<\/a><\/p>\n

\u76ee\u7684<\/h2>\n

\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u3068\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u306eAPI\u3068\u306e\u901a\u4fe1\u3092\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002
\n\u5f0a\u793e\u3067\u306fflux<\/code>\u3092\u30d9\u30fc\u30b9\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u304c\u591a\u304f\u3001\u901a\u4fe1\u90e8\u5206\u306fActionCreators<\/code>\u30ec\u30a4\u30e4\u30fc\u306b\u95a2\u6570\u3068\u3057\u3066\u5b9f\u88c5\u3055\u308c\u3066\u3044\u307e\u3059\u3002
\n\u305d\u306e\u305f\u3081XHR\/http\u30e2\u30b8\u30e5\u30fc\u30eb<\/code>\u3092\u5207\u308a\u66ff\u3048\u308b\u30e6\u30cb\u30d0\u30fc\u30b5\u30eb\u306a\u901a\u4fe1\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u7528\u3044\u3066\u3044\u308c\u3070\u3001\u6bd4\u8f03\u7684\u74b0\u5883\u3078\u306e\u4f9d\u5b58\u304c\u4f4e\u3044\u30c6\u30b9\u30c8\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\u4eca\u56de\u306fmocha<\/code>\u3067\u8d70\u3063\u3066\u3044\u308b\u30c6\u30b9\u30c8\u74b0\u5883\u3067nock<\/code>\u3092\u7528\u3044\u305f\u507d\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8a2d\u5b9a\u3057\u3066\u901a\u4fe1\u6a5f\u80fd\u306e\u30c6\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002<\/p>\n

\u5c0e\u5165<\/h2>\n

\u30c6\u30b9\u30c8\u306b\u7528\u3044\u305f\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u4e3b\u306b3\u3064\u3067\u3059\u3002<\/p>\n

nock<\/h3>\n

http<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u7528\u3044\u305f\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9014\u4e2d\u3067\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u507d\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u30e2\u30b8\u30e5\u30fc\u30eb\u3002<\/p>\n

nock<\/a><\/p>\n

mocha<\/h3>\n

Node.js\/\u30d6\u30e9\u30a6\u30b6\u5171\u7528\u306e\u30c6\u30b9\u30c8\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3002mocha.conf.js<\/code>\u306e\u3088\u3046\u306a\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u306e\u3067\u3001\u624b\u8efd\u306b\u30c6\u30b9\u30c8\u304c\u59cb\u3081\u3089\u308c\u307e\u3059\u3002<\/p>\n

mocha<\/a><\/p>\n

power-assert<\/h3>\n

\u30c6\u30b9\u30c8\u5931\u6557\u6642\u306e\u8c4a\u5bcc\u306a\u60c5\u5831\u91cf\u3068\u30b7\u30f3\u30d7\u30eb\u306aAPI\u304c\u9b45\u529b\u306e\u30c6\u30b9\u30c8\u7d50\u679c\u691c\u8a3c\u30e9\u30a4\u30d6\u30e9\u30ea\u3002<\/p>\n

power-assert<\/a><\/p>\n

\u9069\u7528<\/h2>\n

\u305d\u308c\u3067\u306f\u5b9f\u969b\u306bsuperagent<\/code>\u3092\u4f7f\u3063\u305f\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u507d\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

\u307e\u305a\u306f\u5404\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002
\n\u203bespower-loader\/intelli-espower-loader<\/code>\u306fpower-assert<\/code>\u306e\u88dc\u52a9\u7684\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3060\u3068\u601d\u3063\u3066\u304f\u3060\u3055\u3044\u3002
\n\u306a\u305c\u5fc5\u8981\u306a\u306e\u304b\u306f
\u3053\u306e\u8a18\u4e8b<\/a>\u3092\u53c2\u7167\u3002<\/p>\n

npm install --save-dev mocha power-assert espower-loader intelli-espower-loader nock superagent<\/code><\/pre>\n

\u30c6\u30b9\u30c8\u5bfe\u8c61\u3068\u3057\u3066\u3001\u9069\u5f53\u306aAPI\u3092\u53e9\u3044\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306b\u6e21\u3059\u95a2\u6570\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
\n\u8a71\u3092\u7c21\u5358\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u8a8d\u8a3c\u306e\u4e0d\u8981\u306agithub<\/code>\u306e\u30e6\u30fc\u30b6\u30fcAPI\u3092\u547c\u3076\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n

API\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u3053\u3061\u3089<\/a><\/p>\n

\/\/ \u30c6\u30b9\u30c8\u5bfe\u8c61\u3068\u306a\u308b\u30d5\u30a1\u30a4\u30eb dest.js\nconst request = require('superagent');\nconst url = 'https:\/\/api.github.com\/users';\n\nmodule.exports = (done)=> {\n  request.get(url).end((err, ret)=> {\n    if (err) {\n      return done(err);\n    }\n    done(null, ret.body);\n  });\n};<\/code><\/pre>\n

\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n

\/\/ \u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\nconst nock = require('nock');\nconst assert = require('power-assert');\nconst dest = require('.\/dest.js'); \/\/\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u30d5\u30a1\u30a4\u30eb\n\nconst url = 'https:\/\/api.github.com';\nconst path = '\/users';\n\ndescribe('\u507d\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30c6\u30b9\u30c8', ()=> {\n  \/\/ \u507d\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u8a2d\u5b9a\n  \/\/ \u4e00\u5ea6\u30ea\u30af\u30a8\u30b9\u30c8\u3055\u308c\u308b\u3068\u507d\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u8a2d\u5b9a\u306f\u6d88\u8cbb\u3055\u308c\u3066\u7121\u304f\u306a\u308b\n  nock(url).get(path).reply(200, {\n    isMock: true\n  });\n\n  it('\u507d\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u3063\u3066\u304f\u308b', (done)=> {\n    dest((err, response)=> {\n      assert(err === null); \/\/ \u30a8\u30e9\u30fc\u306b\u306a\u3089\u306a\u3044\n      assert(response.isMock === true); \/\/ \u507d\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u3063\u3066\u304d\u3066\u3044\u308b\n      done();\n    });\n  });\n});<\/code><\/pre>\n

\u5b9f\u884c<\/h2>\n

\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

$(npm bin)\/mocha --require intelli-espower-loader<\/code><\/pre>\n

\u3061\u3083\u3093\u3068\u507d\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u3063\u3066\u304d\u3066\u3044\u3066\u3001\u826f\u3044\u611f\u3058\u3067\u3059\u3002
\n\"\"<\/p>\n

\u307e\u3068\u3081<\/h2>\n

\u4ee5\u4e0a\u3001\u30e2\u30c3\u30af\u30ec\u30b9\u30dd\u30f3\u30b9\u30e9\u30a4\u30d6\u30e9\u30eanock<\/code>\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002
\n\u53c2\u8003\u306b\u3057\u3066\u9802\u3051\u305f\u3089\u3046\u308c\u3057\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6708\u306b1kg\u3072\u3088\u3053\u8c46\u3092\u98df\u3079\u308b\u5c0f\u98fc\u3067\u3059\u3002 \u5148\u65e5\u5f53\u30d6\u30ed\u30b0\u3067\u5ca1\u672c\u304csinon.js\u3067XHR\u3092\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u30e2\u30c3\u30af\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u65b9\u6cd5\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3057\u305f\u3002 \u3061\u3087\u3046\u3069\u79c1\u3082\u5225\u306e\u6848\u4ef6\u3067API\u30b5\u30fc\u30d0\u30fc\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30c6\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u5ca1\u672c\u3068\u306f\u9055\u3063\u305f\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u63a1\u7528\u3057\u307e\u3057\u305f\u3002 \u672c\u7a3f\u3067\u306fNode.js\u306ehttp\u30e2\u30b8\u30e5\u30fc\u30eb\u304b\u3089\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u30e2\u30c3\u30af\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059nock\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002 nock […]<\/p>\n","protected":false},"author":1,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[77,28,166],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/218"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":1,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"predecessor-version":[{"id":3466,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/218\/revisions\/3466"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}