Topic: We don't understand what controller tests do or should do

Hi  Newbie rails programmer is trying to get a handle on testing with rspec.  I have the code below which it turns out passes even if there is no edit method in the controller.  Obviously something is wrong. (The code came from someone else's example) What should we be testing in an edit controller test and how?  Any help gratefully appreciated.  regards dukha

describe CoursesController do
  sign_in_user

  def mock_course(stubs={})
    @mock_course ||= mock_model(Course, stubs).as_null_object
  end

  describe "GET index current_user.current_organisation.accessible_courses" do
    it "assigns all courses as @courses" do
      Course.stub(:all) { [mock_course] }
      pending 'how to stub current_user.current_organisation.accessible_courses ?'
      get :index
      assigns(:courses).should eq([mock_course])
    end
  end

  describe "GET show" do
    pending 'show not used'
    it "assigns the requested course as @course" do
      Course.stub(:find).with("37") { mock_course }
      get :show, :id => "37", :locale => "en"
      assigns(:course).should be(mock_course)
    end
  end

  describe "GET new" do
    it "assigns a new course as @course" do
      Course.stub(:new) { mock_course }
      get :new, :locale => "en"
      assigns(:course).should be(mock_course)
    end
  end

  describe "GET edit" do
    it "assigns the requested course as @course" do
      Course.stub(:find).with("37") { mock_course }
      get :edit, :id => "37", :locale => "en"
      puts "+++++++++++++++"
      assigns(:course).should equal(mock_course)
    end
  end

Re: We don't understand what controller tests do or should do

Make sure there are no filters on CoursesController that might set the instance variable. If you don't have an edit action but still have an edit.html.erb (or perhaps edit.haml) file in your views/courses directory rails will still route the action through the edit erb file and if the instance variable is set in a filter the specs will still pass (likewise, the page will render correctly assuming all the edit page needed was the course instance variable).

Re: We don't understand what controller tests do or should do

I've looked at RSpec some but, as newbie rails programmer myself, learning how to do plain Functional Tests I felt was more important.  The Ruby on Rails Guides is a good place to start. (see Functional Tests for Your Controllers)