[rspec-users] Testing a Create action to make sure the User is assigned to a new Model

Michael magic6435 at gmail.com
Sun Jun 28 06:33:06 EDT 2009


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?

Thanks for any help.

-mike





More information about the rspec-users mailing list