我正在尝试构建一些单元测试来测试我的Rails助手,但我永远不会记得如何访问它们.烦人.建议?
在rails 3中你可以做到这一点(实际上它是生成器创建的):
require 'test_helper' class YourHelperTest < ActionView::TestCase test "should work" do assert_equal "result", your_helper_method end end
当然,Matt Darby的rspec变体也适用于rails 3
你可以在RSpec中做同样的事情:
require File.dirname(__FILE__) + '/../spec_helper' describe FoosHelper do it "should do something" do helper.some_helper_method.should == @something end end
从这里被盗:http://joakimandersson.se/archives/2006/10/05/test-your-rails-helpers/
require File.dirname(__FILE__) + ‘/../test_helper’ require ‘user_helper’ class UserHelperTest < Test::Unit::TestCase include UserHelper def test_a_user_helper_method_here end end
[来自Matt Darby,也是在这个帖子中写的.]你可以在RSpec中做同样的事情:
require File.dirname(__FILE__) + '/../spec_helper' describe FoosHelper do it "should do something" do helper.some_helper_method.should == @something end end
这个帖子有点陈旧,但我想我会回复我用的东西:
# encoding: UTF-8 require 'spec_helper' describe AuthHelper do include AuthHelper # has methods #login and #logout that modify the session describe "#login & #logout" do it "logs in & out a user" do user = User.new :username => "AnnOnymous" login user expect(session[:user]).to eq(user) logout expect(session[:user]).to be_nil end end end