David Zhang
2011-Jul-12  01:38 UTC
[rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test?
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20110711/10e414bf/attachment.html>
David Chelimsky
2011-Jul-16  17:49 UTC
[rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test?
On Jul 11, 2011, at 8:38 PM, David Zhang wrote:> 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.What''s the failure message you''re getting?
Wilson Bilkovich
2011-Jul-16  19:58 UTC
[rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test?
Well, he''s basically got a conditional somewhere: if foo.baz else end ..and he wants to use a proxy object as the return value of ''baz'' instead of nil. Unfortunately, MRI just won''t let you do that, so he needs to set different expectations. That''s my understanding of the question, at least. --Wilson. On Sat, Jul 16, 2011 at 1:49 PM, David Chelimsky <dchelimsky at gmail.com> wrote:> > On Jul 11, 2011, at 8:38 PM, David Zhang wrote: > >> 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. > > What''s the failure message you''re getting? > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2011-Jul-16  20:08 UTC
[rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test?
On Jul 16, 2011, at 2:58 PM, Wilson Bilkovich wrote:> On Sat, Jul 16, 2011 at 1:49 PM, David Chelimsky <dchelimsky at gmail.com> wrote: >> >> On Jul 11, 2011, at 8:38 PM, David Zhang wrote: >> >>> 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. >> >> What''s the failure message you''re getting? >> > Well, he''s basically got a conditional somewhere: > if foo.baz > else > end > ..and he wants to use a proxy object as the return value of ''baz'' > instead of nil. > Unfortunately, MRI just won''t let you do that, so he needs to set > different expectations. > > That''s my understanding of the question, at least. > --Wilson.Depends on the failure message. If it is that current_user is nil, then the fix is to set up or stub the current user. If it''s that the mock object doesn''t respond to restaurant_id= (not restaurant_id, which is what the example is stubbing), then the fix is to stub restaurant_id=. Might end up being both are problems, but I don''t know what else is going on elsewhere in the setup. That''s why I was asking about the failure message :) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110716/65afff13/attachment-0001.html>