0

Please excuse my rustiness, first time touching Rails and this project in quite some time.

Ruby Version: 2.5.0
Rails Version: 5.1.7
RSpec Version: 3.9.3
FactoryBot Version: 6.2.0

This is my scripts_controller_spec.rb file with model creation and the test in question:

require 'rails_helper'

describe ScriptsController, type: :controller do

  userID_1 = User.create!(
    email: '[email protected]',
    password: 'useruser',
    password_confirmation: 'useruser'
  )

  script1 = Script.create!(
    name: 'YWoodcutter',
    skill: 'Woodcutting',
    bot_for: 'TRiBot',
    game_for: 'Oldschool Runescape 07',
    user_id: userID_1.id
  )
  script1.save
  
  describe "GET #index" do
    it "assigns @scripts" do
      get :index
      p script1
      expect(assigns(:scripts)).to eq([script1])
    end
  end

When running the tests, the print line above outputs this, as expected:

#<Script id: 1, name: "YWoodcutter", skill: "Woodcutting", bot_for: "TRiBot", game_for: "Oldschool Runescape 07", user_id: 1, created_at: "2021-10-19 08:29:43", updated_at: "2021-10-19 08:29:43">

However, I get this test failure:

Failures:

  1. ScriptsController GET #index assigns @scripts Failure/Error: expect(assigns(:scripts)).to eq([script1])

    expected: [#<Script id: 1, name: "YWoodcutter", skill: "Woodcutting", bot_for: "TRiBot", game_for: "Oldschool Runescape 07", user_id: 1, created_at: "2021-10-19 08:29:43", updated_at: "2021-10-19 08:29:43">]  
    got: #<ActiveRecord::Relation []>

    (compared using ==)

My scripts_controller.rb index function looks like so:

class ScriptsController < ApplicationController

  def index
    @scripts = Script.order(:created_at)
  end

Let me know if you need any more info, and thanks for your help!

13
  • Where you create script1 is essential. Please update with the relevant spec code. Commented Oct 19, 2021 at 9:16
  • @razvans The script1 creation code is in the same scripts_controller_spec.rb file, and just above the test. Commented Oct 19, 2021 at 9:19
  • Plus the controller code with the index action, any filters, etc Commented Oct 19, 2021 at 9:19
  • Put the entire spec file then. Commented Oct 19, 2021 at 9:19
  • @razvans Updated. Not much more in the script_controller_spec.rb file besides some required lines and empty tests (which I omitted). If you'd like to see the full scripts_controller file, what'd be the best way to show that? Just put the code in the question, or would a GitHub link be cleaner? Commented Oct 19, 2021 at 9:29

2 Answers 2

1

I think the Script object is not getting created before calling the index action. Because of this, you are getting the empty ActiveRecord::Relation. In this situation let! should fix your problem

require 'rails_helper'

describe ScriptsController, type: :controller do

  let!(:user_1) do
    User.create!(
      email: '[email protected]',
      password: 'useruser',
      password_confirmation: 'useruser'
    )
  end

  let!(:script1) do
    Script.create!(
      name: 'YWoodcutter',
      skill: 'Woodcutting',
      bot_for: 'TRiBot',
      game_for: 'Oldschool Runescape 07',
      user_id: user_1.id
    )
  end

  describe "GET #index" do
    before { get :index }

    it "assigns @scripts" do
      expect(assigns(:scripts)).to eq([script1])
    end
  end
end
Sign up to request clarification or add additional context in comments.

Comments

0

Based on the current code, it seems you were not calling all script.

Using

### Controller

@scripts = Script.all.order(:created_at)

### Test

## Should use factories to create the records

  let(:user) do 
    create(:user, email: '[email protected]',
                  password: 'useruser',
                  password_confirmation: 'useruser') 
  end

  let(:script) do 
    create(:script, name: 'YWoodcutter',
                    skill: 'Woodcutting',
                    bot_for: 'TRiBot',
                    game_for: 'Oldschool Runescape 07',
                    user: user) 
  end

should fix it.

8 Comments

Gave this a shot, but unfortunately I'm still experiencing the same results. Made the change to the index method in app/controllers/scripts_controller.rb
some times a spring stop before relaunching the tests helps pick up code changes.
Script.order(:created_at) is the same thing as Script.all.order(:created_at). It's very clear that the OP does not have anything in the DB.
If he didn’t have anything in db, the puts script would not return an model with an id.
@borisrorsvort That's what I'm thinking too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.