我想用konacha为我的backbone.js应用程序做一些DOM测试。我在下面读过一些关于科纳查的文章。
这些条目表明,我应该创建一个视图,并将其放在页面对象中,如下所示。
#= require spec_helper
describe "MyApp.Views.Relationships", ->
beforeEach ->
@view = new MyApp.Views.Relationships()
@page.html(@view.el)
console.log @view.el问题:上面的代码中的但是console.log为@view.el显示了“未定义”,尽管这些代码在实践中工作得很好。
如果有人能帮我的话,我真的很高兴。
这是一些有兴趣的密码。
spec_helper.js.coffee
#= require application
#= require_tree ./support
mocha.ui('bdd')
mocha.ignoreLeaks()
beforeEach ->
@page = $("#konacha")
@sandbox = sinon.sandbox.create()
afterEach ->
@sandbox.restore()views/users/relationships.js.coffee
class MyApp.Views.Relationships extends Backbone.View
el: '#relation-form'
template: JST['users/relationships']
initialize: ->
console.log "init"
@render()
console.log @el
render: ->
@img = $('#loading-image').html()
$(@el).html(@template({img: @img}))
thisrelationships.jst.eco
<button class="btn" disabled="disabled"><%- @img %></button>profile.html.erb(extracted)
#snip#
<% if signed_in? and @user != current_user %>
<div id="relation-form" class="action-button"></div>
<% end %>
#snip#
<script type="text/template" id="loading-image">
<%= image_tag('ajax-loader.gif') %>
</script>
<script type="text/javascript">
$(function () {
new MyApp.Views.Relationships()
});
</script>我想对这些代码做的是处理跟踪按钮,像twitter。
提前谢谢。
发布于 2013-03-05 16:34:43
在规范本身中包含视图所需的模板。类似于:
#= require spec_helper
#= require templates/users/relationships
describe "MyApp.Views.Relationships", ->
beforeEach ->
@view = new MyApp.Views.Relationships()
@page.html(@view.el)
console.log @view.el或者你的模板在哪里。
模板可能仍未定义。如果是这样的话,应用程序可能是在模板加载之前加载的。因此,如果是这样的话,您也可以在spec_helper中删除require应用程序,只需在每个规范中具体地包含您正在测试的比特。
https://stackoverflow.com/questions/14618370
复制相似问题