为什么我的sinon假服务器没有返回任何东西?

时间:2016-04-22 00:41:37

标签: javascript mocha sinon

我正在尝试设置一个假的sinon服务器来测试一些请求。在下面的代码中,我的回调函数永远不会被调用。使用// The largest file size that can be staged to the dataflow service. private static final long MAX_STAGED_FILE_SIZE_BYTES = 10 * 1024 * 1024; /** * Returns the list of .jar/etc files to stage based on the * Options, filtering out any files that are too large for * DataflowPipelineRunner. * * <p>If this accidentally filters out a necessary file, it should * be obvious when the pipeline fails with a runtime link error. */ private static ImmutableList<String> getFilesToStage(MyOptions options) { // Construct a throw-away runner with a copy of the Options to see // which files it would have wanted to stage. This could be an // explicitly-specified list of files from the MyOptions param, or // the default list of files determined by DataflowPipelineRunner. List<String> baseFiles; { DataflowPipelineOptions tmpOptions = options.cloneAs(DataflowPipelineOptions.class); // Ignore the result; we only care about how fromOptions() // modifies its parameter. DataflowPipelineRunner.fromOptions(tmpOptions); baseFiles = tmpOptions.getFilesToStage(); // Some value should have been set. Preconditions.checkNotNull(baseFiles); } // Filter out any files that are too large to stage. ImmutableList.Builder<String> filteredFiles = ImmutableList.builder(); for (String file : baseFiles) { long size = new File(file).length(); if (size < MAX_STAGED_FILE_SIZE_BYTES) { filteredFiles.add(file); } else { logger.info("Not staging large file " + file + ": length " + size + " >= max length " + MAX_STAGED_FILE_SIZE_BYTES); } } return filteredFiles.build(); } /** Runs the processing pipeline with given options. */ public void runPipeline(MyOptions options) throws IOException, InterruptedException { // DataflowPipelineRunner can't stage large files; // remove any from the list. DataflowPipelineOptions dpOpts = options.as(DataflowPipelineOptions.class); dpOpts.setFilesToStage(getFilesToStage(options)); // Run the pipeline as usual using "options". // ... } 输出的测试错误为什么不立即调用回调函数?

Error: timeout of 500ms exceeded. Ensure the done() callback is being called in this test.

1 个答案:

答案 0 :(得分:3)

您需要致电server.respond以完成所有请求。 I found this Gist which gives an example.

这是相关的代码。

server.respondWith("GET", "/something",
                   [200, { "Content-Type": "application/json" },
                    '{ "stuff": "is", "awesome": "in here" }']);

var callbacks = [sinon.spy(), sinon.spy()];

jQuery.ajax({
  url: "/something",
  success: callbacks[0]
});

jQuery.ajax({
  url: "/other",
  success: callbacks[1]
});

console.log(server.requests); // Logs all requests so far
server.respond(); // Process all requests so far