Awesome. I totally get it, but is that how you’re always supposed to
spec
out associations and all the methods that go with an association like
“create” and such? I’m interested in that because I’m specing a lot of
code
that deals heavily with code that has associations going on, yet none of
the
examples of RSpec use I come across have anything about associations and
RSpec. I don’t imagine they’d be handled too differently, but it’s just
something wondered about.
Right now, I’m testing and building up a controller and so far my tests
have
been passing as long as I don’t run into any examples that involve
associations. This code, for example, has been giving me a few issues:
my users_controller_spec.rb (or at least, the relevant part)
describe UsersController do
fixtures :users
before(:each) do
@tiffani = users(:tiffani)
@users = [@tiffani]
@users.stub!(:build).and_return(@tiffani)
@mock_account = mock_model(Account, :users => @users, :save => true)
end
describe “when creating a User” do
it “should return the registration form for adding a User on GET
new” do
User.should_receive(:new).once
get :new
end
it "should render the new User registration form on GET new" do
get :new
response.should render_template("users/new")
end
it "should create a new User and then redirect to that User's
profile on
POST create" do
@mock_account.should_receive(:new)
post :create, :new_user => { :first_name => @tiffani.first_name,
:last_name => @tiffani.last_name,
:email_address =>
@tiffani.email_address }
end
it "should redirect to users/new when User is setup with invalid
data"
end
…
end
And in users_controller.rb:
def create
@new_account = Account.new(params[:account])
@new_user = @new_account.users.build(params[:new_user])
respond_to do |wants|
if @new_account.save
flash[:notice] = "Welcome, #{ @new_user.first_name }!"
wants.html { redirect_to(user_url(@new_user)) }
else
wants.html { render :action => "new" }
end
end
end
When I run the tests the third test fails and RSpec complains that “Mock
‘Account_1003’ expected :new with (any args) once, but received it 0
times”
I’m confused about that since I am calling Account.new in the create
method
on the controller. What’s really wrong here?
Thanks in advance for answering my RSpec questions! 
–Tiffani AB