On Sunday 20 June 2010, David Chelimsky wrote:
> On Jun 20, 2010, at 3:19 PM, Michael Schuerig wrote:
> > Are partial mocks supposed to work in rails-rspec 2.0.0.beta.12?
> >
> > I'm trying to do things like
> >
> > view.stub(:current_user).and_return(@user)
> > view.stub(:current_page?).and_return(false)
> >
> > However, when I do that, the view uses its "ordinary"
> > functionality, in particular, assigns are no longer available as
> > instance variables.
>
> Are you using rails beta 4? Before that release, the view() method
> returned a different object every time.
Yes, 3.0.0.beta4. TestCase#_view caches its return value.
> view.should_receive(:anything_at_all) is specifying rails internals,
> and has to be 100% precise. In this case, what you need to do is
> pretty invasive, and I wouldn't recommend it, but here it is:
>
> require 'spec_helper'
>
> describe "widgets/index.html.erb" do
> before(:each) do
> @widgets = assign(:widgets, [
> stub_model(Widget),
> stub_model(Widget)
> ])
> end
>
> it "renders a list of widgets" do
> view.stub(:_render_partial)
>
> view.should_receive(:_render_partial).with(hash_including(:partial
> => @widgets[0]))
> view.should_receive(:_render_partial).with(hash_including(:partial
> => @widgets[1]))
>
> render
> end
> end
>
> Can I hear a "blech!!!!!!!!!!!!".
>
> Unfortunately, there is not a better way to do this yet, and there
> may not be for some time.
It's okay, if I hide away the yucky bits in a helper method.
# spec/support/partial_helpers.rb
module PartialHelpers
def should_render(options)
options.assert_valid_keys(:partial, :count)
view.should_receive(:_render_partial).
with(hash_including(:partial => options[:partial]))
end
def should_not_render(options)
options.assert_valid_keys(:partial, :count)
view.should_not_receive(:_render_partial).
with(hash_including(:partial => options[:partial]))
end
end
# in spec/spec_helpers.rb
config.include PartialHelpers, :example_group => {
:description => lambda { |description|
# FIXME this is a kludge as there is
# currently (rspec 2.0.0.beta.12)
# apparently no other way to identify a view
# example group.
description =~ /\.html\.erb$/
}
}
Michael
--
Michael Schuerig
mailto:[email protected]
http://www.schuerig.de/michael/
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users