Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rspec testing #28

Closed
glaucocustodio opened this issue Jan 30, 2014 · 9 comments
Closed

Rspec testing #28

glaucocustodio opened this issue Jan 30, 2014 · 9 comments

Comments

@glaucocustodio
Copy link

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.

@caio-ribeiro-pereira
Copy link

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

@mehlah
Copy link
Contributor

mehlah commented Jan 6, 2016

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

@glaucocustodio
Copy link
Author

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

@amatsuda
Copy link
Owner

amatsuda commented Jan 6, 2016

@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!

@mehlah
Copy link
Contributor

mehlah commented Jan 6, 2016

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

@amatsuda amatsuda closed this as completed Jan 6, 2016
@amatsuda
Copy link
Owner

amatsuda commented Jan 6, 2016

Thank you all!

@diofeher
Copy link

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

@diofeher
Copy link

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

@memoht
Copy link
Contributor

memoht commented Jul 13, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants