[rspec-users] [RSpec] [Rails] [BDD]

julian mann julian.mann at gmail.com
Sat Feb 28 14:59:32 EST 2009


My first time here, and I'm pretty new to rspec, and indeed BDD/TDD, so bear
with me

I am trying to spec a fairly simple controller update action for an account.
The account belongs_to :user and user has_one :account.

 the route is PUT: /users/1/account

here's the action

 def update
    @account = @user.account
    respond_to do |format|
      if @account.update_attributes(params[:account])
        flash[:notice] = ' Account was successfully updated.'
        format.html { redirect_to(user_account_path( @user)) }
        format.html { render :action => "edit" }

I'm trying to avoid fixtures, and I'm using rspec's own mock framework

In normal operation the @user instance variable is assigned in a
before_filter (get_user) - so

i figured if I stub that I could then assign user:

before(:each) do
instance_variable_set('@user', mock_model(User))

I tried other variations on this - like instance_variable_set('@user',
User.new)  and others

but @user is never assigned

 -- (actually I did have that bit working when I started this mail, but I
messed it up somehow) .. anyway -  then the problem I couldn't get past
after that - and the reason I started writing this mail was: ....

how do I stub out that user's call to :account  and get it to return an
account, so that update_attributes is called? --

( when I was using mocha.. which I abandoned for other reasons - there was
"any_instance_of" but I can't find similar in rspecs own mocking - and it
felt a bit wishy-washy anyway.)

It seems there is plenty of help around on speccing RESTful controllers, but
not when resources are nested

Any input on this will be much appreciated, and really improve my
understanding of mocking. So thanks in advance!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090228/68f32901/attachment.html>

More information about the rspec-users mailing list