Thatsthati
Thatsthati

Reputation: 1

Rspec Controller test not passing

My controller and my test file are bellow.

controllers/reports_controller.rb:

def index
    @reports = Report.all
  end

specs/controllers/reports_controller_spec.rb:

RSpec.describe ReportsController, type: :controller do
  let(:test_report) {
    2.times.map {
      create(:report, student: create(:student), report_options_attributes: [
        {option: create(:option), note: "ole" }
      ])
    }
  }

  describe "GET #index" do
    before(:each) do
      get :index
    end

    it "should be success" do
      expect(response).to be_success
    end

    it "should render index template" do
      expect(response).to render_template(:index)
    end

    it "should load all reports" do
      expect(assigns(:report)).to match_array test_report
    end
  end

The last test is not working, but it should work. What is wrong with it?

Upvotes: 0

Views: 67

Answers (2)

Milind
Milind

Reputation: 5112

index test is empty..you need to assert something to pass.

can you add.. assert_response :success in index function.

Upvotes: 2

Luiz Henrique
Luiz Henrique

Reputation: 957

Your var is different from the controller. Use reports instead of report like this:

it "should load all reports" do
      expect(assigns(:reports)).to match_array test_report
    end

It should work.

Upvotes: 0

Related Questions