Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用.toHaveBeenCalledWith的茉莉花测试未能通过报名表_Javascript_Jasmine_Single Page Application_Jquery Callback_Jasmine Jquery - Fatal编程技术网

Javascript 使用.toHaveBeenCalledWith的茉莉花测试未能通过报名表

Javascript 使用.toHaveBeenCalledWith的茉莉花测试未能通过报名表,javascript,jasmine,single-page-application,jquery-callback,jasmine-jquery,Javascript,Jasmine,Single Page Application,Jquery Callback,Jasmine Jquery,我正在使用的单页应用程序有一个带有两个表单的登录视图:登录表单和注册表单。以下规范描述了这些表单的测试。我正在使用 登录表单的测试运行成功,但注册表单的测试失败 Error: Expected spy FormSubmitSpy to have been called with \ [ { name : 'John Doe', email : 'firstname.name@email.com', \ password : 'Passw0rd', password_confirm

我正在使用的单页应用程序有一个带有两个表单的登录视图:登录表单和注册表单。以下规范描述了这些表单的测试。我正在使用

登录表单的测试运行成功,但注册表单的测试失败

Error: Expected spy FormSubmitSpy to have been called with \
    [ { name : 'John Doe', email : 'firstname.name@email.com', \
    password : 'Passw0rd', password_confirmation : 'Passw0rd' } ] \
    but it was never called.

    at new jasmine.ExpectationResult (http://localhost:3000/assets/jasmine.js?body=1:114:32)
    at null.toHaveBeenCalledWith (http://localhost:3000/assets/jasmine.js?body=1:1235:29)
    at null.<anonymous> (http://localhost:3000/assets/user_spec.js?body=1:233:24)
    at jasmine.Block.execute (http://localhost:3000/assets/jasmine.js?body=1:1064:17)
    at jasmine.Queue.next_ (http://localhost:3000/assets/jasmine.js?body=1:2096:31)
    at jasmine.Queue.start (http://localhost:3000/assets/jasmine.js?body=1:2049:8)
    at jasmine.Spec.execute (http://localhost:3000/assets/jasmine.js?body=1:2376:14)
    at jasmine.Queue.next_ (http://localhost:3000/assets/jasmine.js?body=1:2096:31)
    at jasmine.Queue.start (http://localhost:3000/assets/jasmine.js?body=1:2049:8)
    at jasmine.Suite.execute (http://localhost:3000/assets/jasmine.js?body=1:2521:14)
为什么这项工作和“正常”实现失败了


以下是对给定情况的简化:

it("should evaluate true", function() {
  var foo = false;
  _.defer(function() {
    foo = true;
  });
  expect(foo).toBeTruthy();
});

您看到这个问题的原因是回调嵌套在其他方法中,可能是jquerybind和jasmine的spy直接覆盖在您的方法上,所以当测试设置好时,您的方法直到下一个勾号才执行

我发现这个页面很有用,因为它描述了你的问题和潜在的解决方法


然而,我发现最好不要测试回调,因为它们可以被视为私有方法。也许您可以测试它的最终结果?

不使用下划线函数进行延迟的Jasmine方法如下:

var flag = false;
...

runs(function() {
  userController.loginView.ui.signInForm.trigger("submit");
  setTimeout(function() { flag = true; }, 1);
}

waitsFor(function() {
  return flag;
}, "Blah this should never happen", 10);

runs(function() {
  expect(callback).toHaveBeenCalledWith({
    name: name,
    email: email,
    password: password,
    password_confirmation: password
  });
}
@Marc是正确的,问题在于使用bind和Javascript将事件“有时/通常/总是”发送到下一个事件循环的方式(它的异步性质是如何工作的),因此,由于您正在监视回调,因此您希望确保编写测试来解释异步行为


由于您的测试是编写的,您可能会面临第一个测试偶尔也无法通过的风险(我很惊讶它能按原样工作)。您正在以非异步方式测试异步事件回调。。。有意义吗?

如果删除SignInfo测试,会发生什么情况?或者在signUpForm测试之后移动它?可能还有其他原因正在停止事件,然后在某些操作后重新触发。在表单提交中似乎不应该刷新页面。如果止动器没有同步触发,那么这个间谍就会失败。
it("should evaluate true", function() {
  var foo = false;
  _.defer(function() {
    foo = true;
  });
  expect(foo).toBeTruthy();
});
var flag = false;
...

runs(function() {
  userController.loginView.ui.signInForm.trigger("submit");
  setTimeout(function() { flag = true; }, 1);
}

waitsFor(function() {
  return flag;
}, "Blah this should never happen", 10);

runs(function() {
  expect(callback).toHaveBeenCalledWith({
    name: name,
    email: email,
    password: password,
    password_confirmation: password
  });
}