"Passing options to TestCase#cell is deprecated" - message sent if any args used #120

Closed
urbanautomaton opened this Issue Apr 20, 2012 · 6 comments

Projects

None yet

2 participants

@urbanautomaton

Hi,

I've got a set of cells that use the build functionality to determine a concrete cell class from a passed argument, like so:

class CompetitionCell < Cell::Rails
  build do |competition|
    klass = (competition.type_name + "_competition_cell").camelize.constantize
  end

  def show(competition)
    raise NotImplementedError
  end
end

class QuestionCompetitionCell < CompetitionCell
  def show(competition)
    @competition = competition
    render
  end
end

When I try to write a test that checks the correct instances are being created, like so:

describe CompetitionCell do
  let(:competition) { mock_model(Competition, :type_name => "question") }
  subject { cell(:competition, competition) }
  it { should be_instance_of QuestionCompetitionCell }
end

I get the following deprecation message:

DEPRECATION WARNING: Passing options to TestCase#cell is deprecated, please use state-args in #render_cell.

From the source code it appears that the deprecation message is sent if any arguments other than the cell name are passed at all. If I can't pass any arguments to TestCase#cell, then it's effectively impossible to test whether the build block is creating a correct instance (well, without getting deprecation warnings, anyway :)). Is this the intended behaviour? From the deprecation text it seems more as if the intention was to deprecate the old implicit options hash usage, in favour of explicit state-args, but I may be misreading it...

Cheers,
Simon

@apotonick
Collaborator

You shouldn't use the #cell method at all in the test but go with the "official" #render_cell:

render_cell(..) do |cell|
  # here is your cell instance you're longing for!
@urbanautomaton

Ah, okay, thanks. Am I right in thinking that this means there's no official way to get a cell instance without also doing a full render pass for a specified state? The above works, but it means the test double I pass to the render_cell call has to support every method used in the state's template, which I'm not so interested in testing at this level.

From my point of view it'd be really nice to be able to test cell instances independently of the render pass (much like you can test Rails controllers without rendering templates). I think I can bodge around it for the moment by directly accessing ::Cell::Rails.create_cell_for, but it feels a bit wrong.

May I ask why TestCase#cell was deprecated originally? I'd be happy to submit a pull request giving users some way to get hold of a cell instance without doing a render pass, but I don't want to trample all over design decisions you've made. :-)

Cheers,
Simon

@apotonick
Collaborator

Using #create_cell_for is just fine for what you're trying to do! On the other hand, #cell does really the same and maybe we should just remove the deprecation warning. This notice was meant to inform people that the cell no longer accepts options at instantiation time but wants dynamic data as state-args in the #render_cell call.

@apotonick
Collaborator
@apotonick apotonick closed this Apr 20, 2012
@urbanautomaton

Fantastic. Thanks very much! And thanks for Cells in general, by the way - it's making such a difference to the maintainability of our views...

Cheers,
Simon

@apotonick
Collaborator

Thanks. Blog about it. Tweet about it. Or http://feedback.railscasts.com/forums/77-episode-suggestions/suggestions/363034-rails-cells :-) Have a great weekend!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment