编译的HAML代码与预期不符

时间:2014-12-14 03:22:52

标签: ruby-on-rails rspec haml

我正在探索使用HAML作为ERB的替代方案来潜在地解决我目前在Rails 4测试中遇到的问题:Error compiling ERB code from string

我正在执行以下代码:

template = 
"= field_set_tag do\n" + 
"    Lorem Ipsum"

expected = '<fieldset>Lorem Ipsum</fieldset>'
actual = Haml::Engine.new(template).render(ActionView::Base.new).gsub("\n", '')    

expect(actual).to eq(expected)

我在测试结果中得到的是:

Failure/Error: expect(actual).to eq(expected)

  expected: "<fieldset>Lorem Ipsum</fieldset>"
       got: "Lorem Ipsum<fieldset></fieldset>"

为什么街区&#34; Lorem Ipsum&#34;在fieldset标记之前渲染?更重要的是,我怎样才能让它正确渲染?

1 个答案:

答案 0 :(得分:1)

你还没有正确设置Haml用于Rails。 Haml提供了自己的一些Rails方法的实现,包括capture使用的field_set_tag。由于未加载此方法,原始capture正在运行,并且块直接输出到缓冲区但未捕获。

解决方案可能就像添加require 'haml/template'一样简单,但是您应该检查RSpec文档并确保以正确的方式使用Rails,我认为haml/template应该自动加载