我创建了一个定制的设计注册控制器,我想用rspec测试它.
我试过一个非常简单的测试:
it "creates a new parent" do
Parent.should receive(:new)
post :create
end
Run Code Online (Sandbox Code Playgroud)
但我得到这个例外:
Failures:
1) Parent::RegistrationsController POST create creates a new parent
Failure/Error: post :create, { :commit => "Daftar",
uncaught throw `warden'
# /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16:in `throw'
# /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16
Run Code Online (Sandbox Code Playgroud)
我已将此行放在我的测试中:
describe Parent::RegistrationsController do
include Devise::TestHelpers
end
Run Code Online (Sandbox Code Playgroud)
我也已经把这一行:
request.env["devise_mapping"] = Devise.mappings[:parent]
Run Code Online (Sandbox Code Playgroud)
有人有想法解决这个问题吗?
我们可以检查/测试Rails中是否存在路由?
例如,我想显示一个导航栏,它应该显示所有菜单的URL,无论URL是否存在.(两者之间不同,破碎的网址会使用不同的样式).
显示每个菜单的代码如下所示:
%li= link_to_unless_current menu.name, :controller => menu.controller, :action => menu.action
Run Code Online (Sandbox Code Playgroud)
但是,如果提供的控制器和操作路由不存在,Rails会投诉并抛出异常.
在我执行上述代码之前,Rails是否有检查功能?也许,看起来像这样:
-if some_function_to_check_route_existence?(:controller => menu.controller, :action => menu.action)
Run Code Online (Sandbox Code Playgroud)
编辑:
我没有使用菜单的"控制器"和"操作"列,而是使用"url"列.现在,我可以这样做:
- if Menu.root
- for menu in Menu.root.self_and_descendants
- if menu.url
%li= link_to_unless_current menu.name, menu.url
- else
%li= menu.name
Run Code Online (Sandbox Code Playgroud)
我也可以使用rails函数来确定路由的存在,例如:
Rails.application.routes.recognize_path
Run Code Online (Sandbox Code Playgroud) 我创建了一个带有视频附件的rails类,我想知道如何获取上传到我的应用程序的视频的长度.我怎样才能做到这一点?