我想用茉莉花来测试我的骨干看法。 我用下划线库生成骨干的意见模板。
出于测试目的,我使用茉莉茉莉花的jQuery
我不能因为观点已经内嵌红宝石加载灯具在茉莉测试。 这里是我的代码
骨干视图
AlbumView = Backbone.View.extend({
template: _.template($('#album-template').html()),
render: function() {
$('#albums').append(this.template(this.model.attributes));
}
});
该视图使用下面的模板
相册样板album_template.html.erb
<script type="text/html" id="album-template">
<a href="<%%= url %>" class="album <%%= is_owner %>">
<%% if (viewable) { %>
<span class="album-cover">
<span class="photo" style="background-image:url('<%%= small_thumbnail_url %>'); width: 160px; height: 160px;">
<span class="glossy-overlay"></span>
</span>
</span>
<%% } %>
<h3><%%= name %></h3>
<p>
<%%= number_of_photos %>
</p>
</a>
</script>
骨干模型
var Album = Backbone.Model.extend({
initialize: function() {
this.view = new AlbumView({model: this});
},
render: function() {
this.view.render();
}
});
茉莉花测试 - album_spec.js
describe('Album view', function(){
beforeEach(function() {
var album = new Album({
"name": "Photo Stream",
"url": "/albums/1",
"id": "2",
"number_of_photos": "172 Photos",
"small_thumbnail_url": "/assets/sections/albums/covers/auto.png",
"viewable": true,
"is_owner": "owner"
});
loadFixtures('albums_fixture.html');
this.view = new AlbumView();
this.view.model = album;
// loadFixtures('albums_fixture.html');
});
describe("Rendering", function() {
it ("produces the correct HTML", function() {
this.view.render();
expect($("#albums")).toExist();
expect(this.view.template).toBeDefined();
});
});
});
该规范加载以下夹具 - album_fixture.html
<div id="albums"> </div>
<script type="text/html" id="album-template">
<a href="<%%= url %>" class="album <%%= is_owner %>">
<%% if (viewable) { %>
<span class="album-cover">
<span class="photo" style="background-image:url('<%%= small_thumbnail_url %>'); width: 160px; height: 160px;">
<span class="glossy-overlay"></span>
</span>
</span>
<%% } %>
<h3><%%= name %></h3>
<p>
<%%= number_of_photos %>
</p>
</a>
</script>
这个测试失败的
期望(this.view.template).toBeDefined();
该灯具被加载,因为这测试通过预期($(“#张专辑”))toExist()。
我的问题是我怎么能加载有嵌入的Ruby意见灯具? 我是能够成功地测试模型和收藏,但我无法测试的意见。