我知道如何为Minitest编写以下样式的测试......
require "minitest_helper" class ApplicationHelperTest < ActionView::TestCase def test_nav_element_for_current_page self.stub(:current_page?, true) do nav_element('Home', '#').must_equal( '<li class="active"><a href="#">Home</li>') end end def test_nav_element_for_non_current_page self.stub(:current_page?, false) do nav_element('Home', '#').must_equal( '<li><a href="#">Home</li>') end end end
...但我想以规格格式编写它。这是我尝试过的,但它不起作用:
require "minitest_helper" describe ApplicationHelper do it "nav_element for current page" do self.stub(:current_page?, true) do nav_element('Home', '#').must_equal( '<li class="active"><a href="#">Home</li>') end end it "nav_element for non-current page" do self.stub(:current_page?, false) do nav_element('Home', '#').must_equal( '<li><a href="#">Home</li>') end end end
如何告诉Minitest ApplicationHelper
应自动包含ActionView::TestCase
?我已经尝试了几件事,但没有运气。
仅供参考,application_helper.rb
包含:
module ApplicationHelper def nav_element(text, path) options = {} options[:class] = 'active' if current_page?(path) link_tag = content_tag(:a, text, href: path) content_tag(:li, link_tag, options) end end
我正在使用这些捆绑的宝石:
* rails (3.2.6) * minitest (3.2.0) * minitest-rails (0.1.0.alpha.20120525143907 7733031)
(请注意,这是minitest_rails
的主要版本(https://github.com/blowmage/minitest-rails)。)
答案 0 :(得分:3)
MiniTest :: Rails直到今天早上才实现ActionView :: TestCase。感谢您引起我的注意! :)
此修复程序将在0.1版本中。现在,更改您的Gemfile并将minitest-rails
链接到我的git repo:
gem "minitest-rails", :git => "git://github.com/blowmage/minitest-rails.git"
修改:现在可以使用了。您的代码应如下所示:
require "minitest_helper"
describe ApplicationHelper do
it "nav_element for current page" do
# Stub with Ruby!
def self.current_page?(path); true; end
nav_element('Home', '#').must_equal(
'<li class="active"><a href="#">Home</a></li>')
end
it "nav_element for non-current page" do
def self.current_page?(path); false; end
nav_element('Home', '#').must_equal(
'<li><a href="#">Home</a></li>')
end
end
这应该就是你需要做的一切。如果您有任何其他问题,请在邮件列表上开始讨论。 https://groups.google.com/group/minitest-rails
答案 1 :(得分:1)
也许使用mintiest-spec-rails,它解决了所有这些必须使用生成器等,并允许现有的Rails单元,功能和集成测试工作,同时允许使用MiniTest :: Spec断言和语法。