1

将 JQuery 选择器传递给 emberjs 中的 click() 方法时,在测试时,元素不会被点击。我正在尝试编写一个简单的验收测试,其中具有 id 和 href 的元素被单击并且 URL 发生更改,然后我断言页面已更改。

我将 id 添加到<a>元素 id="sidebar-user-profile" 中,当我尝试时click($('#sidebar-user-profile'))收到错误“错误:找不到元素 [对象对象]。”。我尝试在页面上通过键入在 Chrome 的控制台中执行相同的操作,$('#sidebar-user-profile')它选择了确切的元素。

在过去的两周里,我一直在努力让它发挥作用,但我别无选择。

编辑:这是有错误的测试:

test('exists and works /home/user-profile', function(assert) {
  assert.expect(1);
  click('#sidebar-user-profile');
  andThen(function() {
    assert.equal(currentURL(), '/home/user-profile');
  });
});

<li>{{#link-to 'home.user-profile' id="sidebar-user-profile"}}User Profile{{/link-to}}</li>

4

1 回答 1

1

Click 采用选择器而不是 jquery 对象,您的测试将类似于:

click('#sidebar-user-profile');
andThen(() => {
  assert.equal(currentURL(), `/myurl/new`, 'Should go to new route');
});

编辑:

test('exists and works /home/user-profile', function(assert) {
  visit('/home'); // route where your link-to is located
  click('#sidebar-user-profile');
  andThen(function() {
    assert.equal(currentURL(), '/home/user-profile');
  });
});
于 2015-06-05T09:34:37.913 回答