所以我正在ActionController::UrlGenerationError
为当前用户管理员服务。link_to
undefined method
我怀疑 rspec 设置有问题。另外我认为可能有问题concat
在视图中它也完美无缺。
相关装饰器代码:
class ArticleDecorator < Draper::Decorator
delegate_all
decorates_association :comments
def navigation_links_list(without)
h.concat list_element { h.link_to h.t('jump_top'), anchor: 'top' } unless without == :top
unless without == :comments_section
h.concat list_element { h.link_to h.t('jump_to_comment_section'), anchor: 'comments' }
end
list_element { h.link_to h.t('jump_to_new_comment'), anchor: 'new_comment' } unless without == :new_comment
end
def show_to_admin
return unless h.admin_signed_in?
h.concat list_element { h.link_to h.t('edit_article'), h.edit_article_path(object) }
list_element do
h.link_to h.t('delete_article'), h.article_path(object),
method: :delete,
data: { confirm: h.t('are_you_sure') }
end
end
[...]
private
[...]
def list_element
h.content_tag :li, yield
end
end
相关规范代码:
require 'rails_helper'
describe ArticleDecorator, type: :decorator do
let(:admin) { FactoryGirl.create(:admin) }
let(:article) { FactoryGirl.create(:article) }
subject { article.decorate }
describe '#show_to_admin' do
context 'with admin signed out' do
it 'returns nil' do
expect(subject.show_to_admin).to eql nil
end
end
end
describe '#navigation_links_list' do
context 'without "top" link' do
it 'returns jump to comments and jump to articles links' do
expect(subject.navigation_links_list(:top)).to eql 'whatever'
end
end
end
end
运行规范的结果:
Failures:
1) ArticleDecorator#show_to_admin with admin signed out returns nil
Failure/Error: return unless h.admin_signed_in?
NoMethodError:
undefined method `admin_signed_in?' for #<#<Class:0x00000005845da0>:0x00000005235a00>
Did you mean? admin_session_url
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/draper-2.1.0/lib/draper/helper_proxy.rb:35:in `block in define_proxy'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/draper-2.1.0/lib/draper/helper_proxy.rb:16:in `method_missing'
# ./app/decorators/article_decorator.rb:15:in `show_to_admin'
# ./spec/decorators/article_decorator_spec.rb:11:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:30:in `block (3 levels) in <top (required)>'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/base.rb:98:in `cleaning'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/rails_helper.rb:29:in `block (2 levels) in <top (required)>'
2) ArticleDecorator#navigation_links_list without "top" link returns jump to comments and jump to articles links
Failure/Error: h.concat list_element { h.link_to h.t('jump_to_comment_section'), anchor: 'comments' }
ActionController::UrlGenerationError:
No route matches {:action=>"index"}
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/draper-2.1.0/lib/draper/helper_proxy.rb:35:in `block in define_proxy'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/draper-2.1.0/lib/draper/helper_proxy.rb:16:in `method_missing'
# ./app/decorators/article_decorator.rb:9:in `block in navigation_links_list'
# ./app/decorators/article_decorator.rb:55:in `list_element'
# ./app/decorators/article_decorator.rb:9:in `navigation_links_list'
# ./spec/decorators/article_decorator_spec.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:30:in `block (3 levels) in <top (required)>'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/base.rb:98:in `cleaning'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# /home/dominikduda/.rvm/gems/ruby-2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/rails_helper.rb:29:in `block (2 levels) in <top (required)>'
这是我的导轨助手:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'database_cleaner'
require 'devise'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include ActionView::Helpers
config.use_transactional_fixtures = true
Draper::ViewContext.test_strategy :fast do
include Rails.application.routes.url_helpers
end
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
有任何想法吗?我现在已经2天不能前进了。
要求的信息:
请注意,如果您的 Devise 模型被称为 Member 而不是 User,例如,那么可用的助手是:
before_action :authenticate_member!
member_signed_in?
current_member
member_session
我解决了与 link_to 方法相关的问题。我更换了:
h.concat list_element { h.link_to h.t('jump_top'), anchor: 'top' } unless without == :top
和:
h.concat list_element { h.link_to h.t('jump_top'), h.article_path(object, anchor: 'top') } unless without == :top
我基本上将 url 生成器添加到锚点(尽管它在视图中正常工作)。我还必须执行以下操作:
before (:each) do
subject.h.output_buffer = ""
end
因为我正在使用concat
并且此方法附加了当前视图输出缓冲区,这在装饰器规范中不可用。
编辑:
来自评论的 Mine 和 Paweł Duda 解决方案有效,但它们产生的链接正在重新加载页面。