1
votes

I know how to write the following style of test for 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

...but I want to write it in spec format. Here is what I have tried, but it does not work:

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

How do I tell Minitest that ApplicationHelper should automatically include ActionView::TestCase? I've tried several things, with no luck yet.

Just for background, application_helper.rb contains:

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

I am using these bundled gems:

  * rails (3.2.6)
  * minitest (3.2.0)
  * minitest-rails (0.1.0.alpha.20120525143907 7733031)

(Please note that this is the head version of minitest_rails (https://github.com/blowmage/minitest-rails).)

2

2 Answers

3
votes

MiniTest::Rails didn't implement ActionView::TestCase until this morning. Thanks for bringing this to my attention! :)

This fix will be in the 0.1 release. For now, change your Gemfile and link minitest-rails against my git repo:

gem "minitest-rails", :git => "git://github.com/blowmage/minitest-rails.git"

Edit: This works now. Your code should look like the following:

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

That should be all you need to do. If you have any other problems please start a discussion on the mailing list. https://groups.google.com/group/minitest-rails

1
votes

Perhaps use mintiest-spec-rails, it solves all this having to use generators, etc and allows existing Rails units, functionals, and integration test to just work while allowing MiniTest::Spec assertions and syntax to be used.