Joe Half Face
Joe Half Face

Reputation: 2333

Rspec controller testing - unexpected errors/not sure how to handle it

I use this tests for FacultiesController:

describe FacultiesController do
  describe "GET show" do
    it "responds successfully with an HTTP 200 status code" do
      get :show
      expect(response).to be_success
      expect(response).to have_http_status(200)
    end
  end

  describe "GET index" do
    it "responds successfully with an HTTP 200 status code" do
      get :index
      expect(response).to be_success
      expect(response).to have_http_status(200)
    end
  end
end

Output:

Failures:

  1) FacultiesController GET show responds successfully with an HTTP 200 status code
     Failure/Error: get :show

     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"faculties"}
     # ./spec/controllers/faculties_controller_spec.rb:5:in `block (3 levels) in <top (required)>'

  2) FacultiesController GET index responds successfully with an HTTP 200 status code
     Failure/Error: @faculties=Faculty.where(:university_id => @university.id).all

     NoMethodError:
       undefined method `id' for nil:NilClass
     # ./app/controllers/faculties_controller.rb:5:in `index'
     # ./spec/controllers/faculties_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

1) When I run rake routes I see

faculty GET    /faculties/:shortcut(.:format)                      faculties#show

So, I suppose I need to specify shortcut with this GET. But how?

I tried add :shortcut=>"test", but this didn't work, also tried params: {:shortcut=>"test"}. Nothing works until I just write "GET faculties/test" and also remove line get :show.

Is that as it should to be?

2)

Failure/Error: @faculties=Faculty.where(:university_id => @university.id).all

         NoMethodError:
           undefined method `id' for nil:NilClass

First of all, it, again, work just fine in browser testing. I use module which helps controller to define @university by subdomain.

How can I pass this to test? Should I just provide some option to get index? By the way, it is all defined in my factories.rb file, as subdomain string is stored in db.

I though may be I should invoke factories new instance in this spec before describe?

university = create(:university)
faculty = create(:faculty)

Thanks in advance.

Upvotes: 0

Views: 549

Answers (1)

Zh Kostev
Zh Kostev

Reputation: 588

1) You should create a new faculty and pass it's id

describe "GET show" do
  let(:faculty) {FactoryGirl.create(:faculty)}
  it "responds successfully with an HTTP 200 status code" do
     get :show, :id => faculty.id #or get :show, :shortcut => faculty.id
    expect(response).to be_success
    expect(response).to have_http_status(200)
  end
end

2) You should create faculty & university & pass it as params

describe "GET index" do
  let(:university) {FactoryGirl.create(:university)}
  before(:each) do
    FactoryGirl.create(:faculty, :university_id => university.id)  
  end 
  it "responds successfully with an HTTP 200 status code" do
    get :index, university_id => university.id
    expect(response).to be_success
    expect(response).to have_http_status(200)
  end
end

Please provide index route if it wouldn't work

Upvotes: 1

Related Questions