Topic: Tests Failing and dont know why!

My test code:

  Tests the edit action. The problem is "it should be successful" fails but the page loads without error. Render template fails and again the page loads without error.

  # 
  # GET /admin_events/1/edit
  #
  describe "handling GET /admin_events/1/edit" do
 
    before(:each) do
      @event = mock('Event')
      Event.any_instance.stubs(:find_by_id).returns(@event)
    end
   
    def do_get
      get :edit, :id => 1
    end
   
    it "should be successful" do
      do_get
      response.should be_success
    end
   
    it "should render edit template" do
      do_get
      response.should render_template('edit')
    end
   
    it "should find the event requested" do
      Event.expects(:find_by_id).returns(@event)
      do_get
    end
   
    it "should assign the found event for the view" do
      do_get
      assigns[:event] = @event
      assert_equal assigns[:event], @event
    end
   
  end

My controller code:

  # GET /admin_events/1/edit
  def edit
    id = params[:id]
    @event = Event.find_by_id(id) if valid_id?(id)
     
    if @event.nil?
      flash[:error] = 'You are trying to edit an event which doesnt exist!'
      redirect_to(admin_events_url)
    end 
  end

***Forgot test results****

1)
'Admin::EventsController handling GET /admin_events/1/edit should render edit template' FAILED
expected "edit", got nil

2)
'Admin::EventsController handling GET /admin_events/1/edit should be successful' FAILED
expected success? to return true, got false

Last edited by zbo09 (2008-01-23 11:58:55)

Re: Tests Failing and dont know why!

Solved it!

it "should render edit template" do
  Event.expects(:find_by_id).with('1').returns(@event) # added !!!
  do_get
  response.should render_template('edit')
end

***edit***

removed "with('1')" as id is sent in "do_get"


it "should render edit template" do
  Event.expects(:find_by_id).returns(@event) # added !!!
  do_get
  response.should render_template('edit')
end

Last edited by zbo09 (2008-01-23 12:18:31)