我正在尝试使用正则表达式来验证我的Rails模型中的域名格式.我已经使用域名http://trentscott.com测试了Rubular中的正则表达式并且匹配了.
当我在我的Rails应用程序中测试它时,它知道为什么它验证失败(它说"名称无效").
码:
url_regex = /^((http|https):\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$/ix
validates :serial, :presence => true
validates :name, :presence => true,
:format => { :with => url_regex }
Run Code Online (Sandbox Code Playgroud) 我正在使用authlogic进行用户身份验证,在ApplicationController中,我定义了“ current_user”,“ current_user_session”等并将其设置为helper_methods。
对于我的主要索引,我有一个非常简单的视图规格:
RSpec.describe "main/index.html.erb", :type => :view do
context "when not logged in" do
before do
allow(view).to receive(:current_user).and_return(nil)
end
it "has an h1" do
render
expect(rendered).to include('h1')
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是,如果在我的配置中“ mocks.verify_partial_doubles = true”,那么在转储整个对象然后在底部说时,这将引起巨大的错误:
1) main/index.html.erb when not logged in has an h1
Failure/Error: allow(view).to receive(:current_user).and_return(nil)
#<#<Class:0x00000104c249d0>:.........
@rendered_views={}>> does not implement: current_user
Run Code Online (Sandbox Code Playgroud)
当然,建议将verify_partial_doubles设置为true,但是这样做会中断。我直接从文档中提取了以下内容:
如果该方法出现在ApplicationHelper中,它将起作用。但是,如果它在ApplicationController中并定义为helper_method,那么就没有这种运气了:
helper_method :current_user, ...
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
Run Code Online (Sandbox Code Playgroud)
我想要verify_partial_doubles提供的保护,如何解决此问题?