amatsuda/active_decorator

Rspec testing

Closed this issue · 9 comments

If automatically mixes decorator module into corresponding model only when:

  • passing a model or collection of models or an instance of ActiveRecord::Relation from controllers to views
  • rendering partials with models (using :collection or :object or :locals explicitly or implicitly)

How could I to test my decorator's methods with RSpec? Check it out https://gist.github.com/glaucocustodio/81e2d036c67b893c1e6a

Thank you.

I have the same problem! How can I test it?

Almost 2 years after, in case anyone still needs help with this, here is a code snippet for RSpec

describe '#full_name' do
  it 'returns the full organization name' do
    organization = create(:organization, first_name: 'John', last_name: 'Doe')
    decorated_organization = ActiveDecorator::Decorator.instance.decorate(organization)

    expect(decorated_organization.full_name).to eq('John Doe')
  end
end

With the simple decorator

module OrganizationDecorator
  def full_name
    "#{first_name} #{last_name}"
  end
end

Would be nice add some rspec examples like above in readme.md or wiki @amatsuda.

@glaucocustodio Thanks for the suggestion. I'm not sure whether people are still using rspec :trollface:, but yes, any kind of documentation PRs are absolutely welcomed!

@amatsuda PR opened at #55.
This issue and #9 could be closed ;-)

Thank you all!

I still receive that problem by trying to test with MiniTest...

decorated = ActiveDecorator::Decorator.instance.decorate(account)
assert_equal 'Jim, b4, 1 plays | Mary, b4, 0 plays', decorated.children(' | ')

I receive:

(byebug) decorated.children(',')
*** NoMethodError Exception: undefined method `children' for #Account:0x007f98031daea0

nil

It would be nice to add how to test for minitest. The default class created by ActiveDecorator doesn't work.

I realize this issue is closed but thought I would add my comment here. I started using this gem and let it generate a Decorator and corresponding RSpec spec. My first test immediately failed because the RSpec prefix wasn't there. Based on the following comment, I was wondering why the generator for this gem was defaulting to the monkey-patched syntax.

In a concerted effort to keep monkey patching to a minimum. The default Rails spec generators should not use the monkey patched version of describe. Always using only the non-monkey patched RSpec.describe

rspec/rspec-rails@ca0d249