On Tue, Sep 29, 2009 at 2:52 PM, Curran <c.schie...@gmail.com> wrote:
> I'd like to write the following test inside a functional test
> (subclass of ActionController::TestCase):
>
> should_change("level_id", :to => levels(:king).id) \
> { Person.find(people(:joe).id).level_id }
Ooh, is this a game? King me!
> But I get a NoMethodError for levels.
Brian explained this.
> Most importantly, is there another way to accomplish this test
> statement? :)
We'd usually prefer to write:
should "update person" do
person = Factory(:non_king_person)
Person.stubs(:find => person)
person.stubs(:update_attributes).returns(true)
sign_in_as person
params = { 'some' => 'param' }
put :update, :id => person.to_param, :params => params
assert_received(Person, :find) { |expect| expect.with(person.to_param) }
assert_received(person, :update_attributes) { |expect| expect.with(params) }
assert_response :redirect
assert_redirected_to person_url(person)
end
The difference is you're testing that the controller calls the
expected interfaces to the model, and not that the database record is
changed. You can be confident that ActiveRecord's update_attributes
works as expected, so you don't need to actually save the record or
generally test database transactions at the functional test level.
--
Dan Croak
@Croaky
The difference is you're testing that the controller calls the
expected interfaces to the model, and not that the database record is
changed. You can be confident that ActiveRecord's update_attributes
works as expected, so you don't need to actually save the record or
generally test database transactions at the functional test level.