1
我有這個在我application_helper.rb:測試與sanitize方法
def bbcode(text)
# Code snippets
text.gsub!(/\[code=?["']?(.*?)["']?\](.*?)\[\/code\]/mis) { CodeRay.scan($2.strip, $1.to_sym).div(:line_numbers => :table)}
text = sanitize(text, :tags => %w(span div table tr td br pre tt), :attributes => %w(id class style))
# Gist embedding
text.gsub!(/\[gist\](.*?)\[\/gist\]/) { $1.split(" ").map { |gist| "<script src='http://gist.github.com/#{gist}.js'></script>" } }
# allows for nested quotes
bbquote(text)
# non-attributed quote
text.gsub!(/\[quote\](.*?)\[\/quote\]/mis) { "<div class='center'><div class='quote'>" << $1 << "</div></div>" }
# Terminal example
text.gsub!(/\[term\](.*?)\[\/term\]/mi) { "<span class='term'>" << $1.gsub(/^\r\n/,"").gsub("<","<").gsub(">",">") << "</span>" }
# URLs
text.gsub!(/\[url=["']?(.*?)["']?\](.*?)\[\/url\]/mis) { "<a rel='nofollow' href='" << $1 << "'>" << $2 << "</a>" }
# handle with care...
bbcode_ext(text)
end
作爲一個優秀的Rails開發者,我已經嘗試寫此方法測試:
require File.dirname(__FILE__) + '/../spec_helper'
describe ApplicationHelper, "general" do
include ApplicationHelper
it "should correctly encapsulate double quotes" do
bbcode('[quote="Kitten"][quote="Dog"]QUOTE INSIDE[/quote]QUOTE OUTSIDE[/quote]').should eql("<div class='center'><div class='quote'><b>Kitten wrote:</b><div class='center'><div class='quote'><b>Dog wrote:</b></div></div>QUOTE OUTSIDE</div></div>")
end
end
當我去運行這個測試但是我得到這個錯誤undefined method 'white_list_sanitizer' for Spec::Rails::Example::HelperExampleGroup::Subclass_1:Class
這是因爲Spec::Rails::Example::HelperExampleGroup::Subclass_1:Class
!= ActionView::Base
。我如何測試使用清理方法的代碼?
作品上運行您的規格,非常感謝! – 2009-08-30 03:49:41