因此,我为ActionController::UrlGenerationError
获取link_to
,为当前用户admin获取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łDudasollution工作,但他们生成了重新加载页面的链接。