So I am getting ActionController::UrlGenerationError for link_to and undefined method for current user admin.
I suspect that something is wrong with rspec setup. Also I think there might be a problem with concat
Also in views it works flawelessly.
Relevant decorator code:
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
Relevant spec code:
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
Result of running spec:
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)>'
Here is my rails helper:
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
Any ideas? I can't move forward for 2 days now.
Requested information:
From devise github page
Notice that if your Devise model is called Member instead of User, for example, then the helpers available are:
before_action :authenticate_member!
member_signed_in?
current_member
member_session
I solved issue releated to link_to methodd. I replaced:
h.concat list_element { h.link_to h.t('jump_top'), anchor: 'top' } unless without == :top
with:
h.concat list_element { h.link_to h.t('jump_top'), h.article_path(object, anchor: 'top') } unless without == :top
I basically added url generator to anchor (It was working properly in views though). Also I had to do following:
before (:each) do
subject.h.output_buffer = ""
end
because I am using concat and this method appends current view output buffer which is not available in decorator spec.
EDIT:
Mine and Paweł Duda sollution from comments works, but they produce links which are reloading page.