我需要验证jqxgrid' rowClicked'使用EmberJS中的mocha单元测试调用action。我初始化了一个网格,可以验证它是否被渲染,渲染了行和标题,但我仍然坚持使用rowclick事件。我使用jQuery模拟这样一行的点击:
T
我的网格代码会侦听rowClick事件,如下所示:
this.$('#row0grid_testgrid').trigger('click');
我如何验证这个被调用? 感谢
答案 0 :(得分:0)
你能做这样的事 - 模拟功能吗?
on()
这里有一些事情需要提及:如果这样做,你将测试// My code here
已被调用,并且它是在正确的动作'rowclick'上触发的。但是,您仍然无法在事件函数中测试代码“/*** your component code ***/
// this will be called on "rowclick"
myComponentFunction: function(whatArgument){
// My code here
}
....
const self = this;
this.grid().on('rowclick', function(evt) {
// instead of pure code call a function
const someParameters = "foo";
self.myComponentFunction(someParameters);
});
...
”的一部分。
如果你想测试你的事件功能,你可以做的是从它调用匿名函数。让我告诉你我的意思:
myComponentFunction
在您的单元测试中,您可以同时模拟// same unit test
....
const originalMyComponentFunction = gridComponent.myComponentFunction;
gridComponent.myComponentFunction = function(arg){
assert.ok(true, "myComponentFunction() has been called!");
// test argument, whatever
assert.equal(arg, "foo", "argument passed to myComponentFunction() from an event triggered on 'rowclick' is correct");
}
// tidy up myComponentFunction mock, too.
gridComponent.myComponentFunction = originalMyComponentFunction;
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:elevation="4dp"
>
</android.support.v7.widget.Toolbar>
顺便说一下,设置模拟并整理它们的首选方法是将它放入beforeEach()和afterEach()中,查看ember-cli testing guides。
如果您有更好的想法如何测试,我也想向您学习:)