qunit-除了格式化外,文件的测试功能
UI5在测试格式函数的UI5给出的示例IE
// Assert
assert.strictEqual(fnIsolatedFormatter("A"), "New", "The long text for status A is correct");
assert.strictEqual(fnIsolatedFormatter("B"), "In Progress", "The long text for status B is correct");
assert.strictEqual(fnIsolatedFormatter("C"), "Done", "The long text for status C is correct");
assert.strictEqual(fnIsolatedFormatter("Foo"), "Foo", "The long text for status Foo is correct");
效果很好,但是,适用于控制器文件的函数IE example.controller.js
,产生错误>错误。
通过尝试测试_ontestFunction
example.controller
i确实得到以下错误
:
TypeError: Example._onTestFunction is not a function
问题:如何我测试不是格式>格式化
的控制器文件的功能?
The example given by UI5 of testing the formatter functions i.e.
// Assert
assert.strictEqual(fnIsolatedFormatter("A"), "New", "The long text for status A is correct");
assert.strictEqual(fnIsolatedFormatter("B"), "In Progress", "The long text for status B is correct");
assert.strictEqual(fnIsolatedFormatter("C"), "Done", "The long text for status C is correct");
assert.strictEqual(fnIsolatedFormatter("Foo"), "Foo", "The long text for status Foo is correct");
Works out very well, however, the same testing applied to functions of a controller file i.e. Example.controller.js
, produces an error
.
By trying to test the _onTestFunction
of the Example.controller
i do get the following error
:
TypeError: Example._onTestFunction is not a function
Question: How do i test functions of a controller file that isn't formatter
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要测试控制器,您需要实例化一个:
令ControllerDerneTest = New Controller(...)
。如果控制器具有任何依赖关系(通常是这种情况),则需要嘲笑这些依赖项。为此,请使用诸如sinon
之类的框架。然后,您可以在每个测试中指定这些模拟应如何行为。实例化控制器后,您可以执行方法并添加断言。在您的情况下,您似乎正在尝试调用
_onteStfunction
不是在对象上,而是在控制器的“定义”上调用。这不像预期的那样包含该方法,并导致错误。To test a controller, you need to instantiate one:
let controllerUnderTest = new Controller(...)
. If the Controller has any dependencies (as this is usually the case), these dependencies need to be mocked. For this, use a framework such assinon
. Then you can specify in each test how these mocks should behave. Once you have instantiated the controller, you can execute a method and add assertions.In your case, it looks like you are trying to call the
_onTestFunction
not on an object, but on the "definition" of the Controller. This does not contain the method, as expected, and results in the error.