Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSpec controller GET #index test not recognizing existence of created model. #2527

Closed
AakLak opened this issue Oct 19, 2021 · 2 comments
Closed

Comments

@AakLak
Copy link

AakLak commented Oct 19, 2021

This is more of a help request than an issue, let me know if there's a better place I can direct this.
I also posted this on StackOverflow

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

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.5.0
Rails version: 5.1.7
RSpec version: 3.9.3

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: 'ueferfrfrf@u1.com',
    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

Here's the full repository - https://github.com/AakLak/rs_script_stats
Let me know if you need any more info, and thanks for your help!

@AakLak
Copy link
Author

AakLak commented Oct 19, 2021

They way I was creating my objects was only saving them to memory, but not the database. I needed to use a let or before block to do that.

@AakLak AakLak closed this as completed Oct 19, 2021
@pirj
Copy link
Member

pirj commented Oct 20, 2021

It's a bad practice to create records outside the example scope.
By the example scope, I mean inside example groups or before hooks.
See this discussion rubocop/rubocop-rspec#1648

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants