I'm wondering how you tell RSpec to ignore null objects entirely.
For instance, I have this restaurant_id call below:

  def create
    @meal = Meal.new(params[:meal])
    @meal.restaurant_id = current_user.restaurant_id # automatically decide 
the restaurant-meal relationship
    if @meal.save
      flash[:notice] = "The meal was saved successfully."
      redirect_to :action => :index
    else
      render :action => :new
    end
  end

...and I'm not sure how to deal with it.  My spec test looks like this right 
now:

  describe "POST create" do
    let(:meal) { mock_model(Meal).as_null_object }
    
    before(:each) do
      Meal.stub(:new).and_return(meal)
      # meal.stub(:restaurant_id) Not sure what to do here!!!!!!!!
    end
    
    it "creates a new meal" do
      Meal.should_receive(:new).with("name" => "Pizza").and_return(meal)
      post :create, :meal => { "name" => "Pizza" }
    end
    
    context "when the meal saves successfully" do
      before(:each) do # just for balance; as_null_object causes a truthy 
value to be returned anyway
        meal.stub(:save).and_return(true)
      end
      
      it "sets a flash message" do
        post :create
        flash[:notice].should eq("The meal was saved successfully.")
      end
      
      it "redirects to the meals index" do
        post :create
        response.should redirect_to(:action => :index)
      end
    end
           
    context "when the meal fails to save" do
      before(:each) do
        meal.stub(:save).and_return(false)
      end
      
      it "assigns @meal" do
        post :create
        assigns[:meal].should eq(meal)
      end
      
      it "renders the new template" do
        post :create
        response.should render_template("new")
      end
    end
    
  end

If anyone knows the best way to deal with this, please let me know.  I 
really want to adeptly use RSpec in the future.
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to