[rspec-users] Testing a Create action to make sure the User is assigned to a new Model
Matt Wynne
matt at mattwynne.net
Sun Jun 28 17:11:17 EDT 2009
On 28 Jun 2009, at 11:33, Michael wrote:
> I'm trying to figure out how to test that the current_user does end up
> being attached to a new model that is being created which happens to
> be an Album.
>
> Here is the Controller code.
>
> def create
> @album = current_user.albums.new params[:album]
> if @album.save
> flash[:notice] = "The album was saved successfully."
> redirect_to @album
> else
> render :action => :new
> end
> end
>
> and here is a quick overview of how i have been testing so far.
>
> before(:each) do
> @album = mock_model(Album, :save => nil)
> Album.stub(:new).and_return(@album)
> end
>
> describe "authenticated user" do
>
> before(:each) do
> activate_authlogic
> UserSession.create Factory.build(:valid_user)
> end
>
> it "should build new album" do
> Album.should_receive(:new).with("title" => "album title",
> "description" => "album description").and_return(@album)
> post :create, :album => {"title" => "album title", "description"
> => "album description"}
> end
>
> it "should save the album" do
> @album.should_receive(:save)
> post :create
> end
>
> end
>
> How would i test to make sure that on the save "user_id" is present
> and matches the logged in user?
From the code you've shown us, it looks as though you need to spec
the view, since I presume that's where the params[:album] which may or
may not contain this user_id is built. The controller is just passing
these params directly to the model's constructor, so it doesn't really
seem to have much of a part to play as regards this specific field.
This might be a case where you'll get more value from a full-stack
Cucumber acceptance test.
cheers,
Matt Wynne
http://mattwynne.net
+447974 430184
More information about the rspec-users
mailing list