#store_params is not an “action”, therefore I would make it private.
I agree completely, and probably should have specified that in my sample
code
(it certainly is private in my real non-hypothetical implementation).
Since it is used only by the #create action, I would spec it as part of the
#create spec:
Well this came up for me because I have a bit of a complex controller
which has
several before_filters, and I wanted a clean way to really verify that
each and
every filter is doing what it should, so that’s why I wanted to spec
those
individual methods.
You can, but you should mock Post.create!:
describe ‘#create’ do
it ‘stores the :post params’ do
Post.should_receive(:create!).and_return(true)
post :create, { :post => { :fake_param => “foobar” } }
session[:post_params][:fake_param].should == “foobar”
end
end
Unfortunately, this project is using a plugin called “Decent Exposure”
which
does various magic to simplify controller code (and honestly has been a
big
headache). So, the create action actually calls post.save!
the ‘post’ that .save! is called on is actually auto-generated by a
method
created by Decent Exposure. It returns a new record pre-populated with
params[:post]…
The failure I see is:
Failure/Error: post :create, { :post => { :fake_param => "foobar" }
}
ActiveRecord::UnknownAttributeError:
unknown attribute: foo
# ./app/controllers/posts_controller.rb:107:in `new’
… So, I can’t stub out “create!”, because this failure is happening
before
there… And, if I do: Post.stubs(:new).returns(true)
I still get the same failure.
I even took it a step further where I explicitly told decent exposure to
use a
method:
expose(:post) do
exposed_for_post if params[:post]
end
def exposed_for_post
Post.new(params[:post])
end
… And guess what? Doing subject.stubs(:exposed_for_post).returns true
still gives me the same failure, unknown attribute: foo in ‘new’…
But,
manually testing stuff proves that exposed_for_post method IS being
called, and
everything is working… I just want an automated test to prove it.
I am totally stumped on this one…
Patrick J. Collins
http://collinatorstudios.com