Jquery 在Banckbone中模拟HTML元素进行Jasmine测试

Jquery 在Banckbone中模拟HTML元素进行Jasmine测试,jquery,templates,backbone.js,jasmine,Jquery,Templates,Backbone.js,Jasmine,我正在做主干应用程序的Jasmine单元测试。我对茉莉花和脊梁骨都是新手。我在应用程序中的视图是使用BackboneJS从HTML模板创建的 因为应用程序已经部署。现在我想测试主干视图。为此,我需要创建一个 文本框中包含一些模拟数据的模拟HTML 我有以下代码: that.uoView = new UoView(); that.uoView.render(); $('#sandbox').html(that.uoView.el); that.uoView = new UoView(); tha

我正在做主干应用程序的Jasmine单元测试。我对茉莉花和脊梁骨都是新手。我在应用程序中的视图是使用BackboneJS从HTML模板创建的

因为应用程序已经部署。现在我想测试主干视图。为此,我需要创建一个 文本框中包含一些模拟数据的模拟HTML

我有以下代码:

that.uoView = new UoView();
that.uoView.render();
$('#sandbox').html(that.uoView.el);
that.uoView = new UoView();
that.uoView.render();
$('#sandbox').html(that.uoView.el);
$("#test").val('Test'); //#test is an element in uoView.el
在我的茉莉花档案里。这是我唯一可以更改的文件。它调用一个视图并将其保存在$'sandbox'中


因为我已经有了HTML模板,所以我需要在HTML文本框中填充一些模拟数据。

通常使用如下元素初始化视图:

that.uoView = new UiView({ el: $("#myelement") });
that.uoView.render()
然后在render函数中,呈现el中应包含的html:


每次调用render时,都会再次渲染视图。您可以在任何html事件上调用render。

您可以在渲染后返回此消息

render: function() {
  this.$el.html('my html');
  return this;
}
因此,你最终会:

that.uoView = new UoView();
$('#sandbox').html(that.uoView.render().el);

简单Jquery按照rednaw的建议工作。编写此代码后:

that.uoView = new UoView();
that.uoView.render();
$('#sandbox').html(that.uoView.el);
that.uoView = new UoView();
that.uoView.render();
$('#sandbox').html(that.uoView.el);
$("#test").val('Test'); //#test is an element in uoView.el
我添加了以下代码:

that.uoView = new UoView();
that.uoView.render();
$('#sandbox').html(that.uoView.el);
that.uoView = new UoView();
that.uoView.render();
$('#sandbox').html(that.uoView.el);
$("#test").val('Test'); //#test is an element in uoView.el

然后是我的规格

我已经调用了我的渲染,无法对渲染函数进行任何更改。在我得到el中的值之后。现在我想对DOM元素进行某些更改。我说的是jquery,也许在询问之前先自己尝试一下。我已经调用了我的render,无法对我的render函数进行任何更改。在我得到el中的值之后。现在我想对DOM元素进行某些更改。您可以在uoView中创建一个函数,该函数描述您的DOM更改示例:add_tooltip:function{//do stuff on$el并调用this.render}这正是我要查找的。但是怎么做呢。?我不知道该怎么做。你可以通过扩展视图对象来创建一个视图,然后将你所有的代码放在其中。从文件上看:这已经完成了。。我使用Jasmine进行单元测试,在那里我需要创建一个模拟元素,我使用现有HTML模板中的$el属性创建它。现在我需要用一些数据来模拟HTML。为了帮助人们在将来回答您的问题,请提供所有相关的代码。例如,我在问题中没有看到id为officeToUnlock的元素。@rednaw:officeToUnlock只不过是uoView.el中的一个元素