使用 rSpec 测试机架路由 [英] Testing Rack Routing Using rSpec
问题描述
我的 routes.rb 中有一条规则:
I have a rule in my routes.rb:
constraints AssetRestrictor do
match '*seopath' => SeoDispatcher
end
然后在 lib/seo_dispatcher.rb 中,我有这个:
Then in lib/seo_dispatcher.rb, I have this:
class SeoDispatcher
AD_KEY = "action_dispatch.request.path_parameters"
def self.call(env)
seopath = env[AD_KEY][:seopath]
if seopath
params = seopath.split('/') # get array of path components
env[AD_KEY][:id] = params.last # the real page name is the last element
env[AD_KEY][:category] = params.first if params.length > 1
end
Rails.logger.debug "routing to show #{env[AD_KEY]}"
PagesController.action(:show).call(env)
# TODO error handling for invalid paths
end
end
class AssetRestrictor
EXCEPTION_FILES = ['javascripts', 'stylesheets', 'autodiscover']
def self.matches?(request)
return false if request.method == 'POST' # no post requests are SEO-ed
EXCEPTION_FILES.each do |ex|
return false if request.url.include?(ex)
end
true
end
end
基本上,整个事情都奏效了.这个想法是剥离最后一个路径组件并将其与页面的 slug 匹配.搜索引擎优化人员告诉我,这是诱使搜索引擎将您排名更高的很酷的方法.
And basically, the whole thing works. The idea is to peel off the last path component and match it to the slug for a page. SEO people tell me this is the cool way to trick search engines into ranking you higher.
撇开我尖刻的评论不谈,我在 rSpec 中编写测试来执行此代码时遇到了麻烦.我的第一枪是:
My snarky comments aside, I'm having trouble writing a test in rSpec that exercises this code. My first shot at it was:
describe SeoDispatcher do
it "routes /insurance/charters-and-guides/how-to-buy-charter-boat-insurance to pages#show :id => how-to-buy-charter-boat-insurance" do
{ :get => "/insurance/charters-and-guides/how-to-buy-charter-boat-insurance"}.should route_to(
:controller => 'pages',
:action => 'show',
:id => 'how-to-buy-charter-boat-insurance'
)
end
end
但这根本不执行机架调度代码.有谁知道一个人将如何(而不是 cuking)练习代码?我真的很想从 Rack 中引入参数,而不是仅仅执行 SeoDispatcher.call({"action_dispatch.request.path_parameters" => {:seopath => "/foo/bar"}})
.
But this simply doesn't exercise the Rack dispatch code. Does anyone know how one would (than cuking it) exercise the code? I really want to bring the parameters in from Rack instead of just doing SeoDispatcher.call({"action_dispatch.request.path_parameters" => {:seopath => "/foo/bar"}})
.
谢谢!
推荐答案
好的,回答了我自己的问题.请求规范:
Ok, answered my own question. Request spec:
describe SeoDispatcher do
describe "seo parsing" do
it "GET /insurance/charters-and-guides/how-to-buy-charter-boat-insurance displays how-to-buy-charter-boat-insurance (3-part path)" do
p = Page.make(:title => "how-to-buy-charter-boat-insurance")
p.save
get "/insurance/charters-and-guides/how-to-buy-charter-boat-insurance"
request.path.should == "/insurance/charters-and-guides/how-to-buy-charter-boat-insurance"
assigns[:page].slug.should == "how-to-buy-charter-boat-insurance"
end
it "GET /insurance/charters-and-guides displays guides (2-part path)" do
p = Page.make(:title => "charters and guides")
p.save
get "/insurance/charters-and-guides"
request.path.should == "/insurance/charters-and-guides"
assigns[:page].slug.should == "charters-and-guides"
end
it "GET /insurance displays insurance (1-part path)" do
p = Page.make(:title => "insurance")
p.save
get "/insurance"
request.path.should == "/insurance"
assigns[:page].slug.should == "insurance"
end
end
end
如果有人知道更好的方法,请随时告诉我!
If anyone knows a better way, feel free to let me know!
这篇关于使用 rSpec 测试机架路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!