简体   繁体   中英

Rspec controller assigns doesn't work as expected

In this project I use:

gem 'rails', '~> 5.1.1' 
gem 'mongoid', '~> 6.1.0' 
gem 'rspec-rails', '~> 3.7'

I have model Book:

class Book
  include Mongoid::Document
  field :name, type: String
  field :author, type: String
  field :description, type: String
  field :status, type: String
  field :image, type: String

  has_many :histories, dependent: :destroy
  has_many :likes, dependent: :destroy
  has_many :comments, dependent: :destroy

  validates :name, :author, :description, :status, :image, presence: true

  def ordered_histories
    histories.order(taken_in: :desc)
  end

  def book_rating
    rating_array = likes.map { |like| like.rate.to_i }
    return 0 if rating_array.empty?
    (rating_array.sum / rating_array.size).round
  end
end

In Books controller I have only Index and Show actions. Here is code, related to index action of Books controller:

class BooksController < ApplicationController
  before_action :set_top_books, only: :index

  # GET /books
  # GET /books.json
  def index
    @books = Kaminari.paginate_array(Book.all).page(params[:page])
  end

  private

  def set_top_books
    @top_books = Book.all.order_by(likes_count: :desc, histories_count: :desc).limit(5)
  end
end

I have Factory for Book. Here is code:

FactoryBot.define do
  factory :book do
    name { Faker::Book.title }
    author { Faker::Book.author }
    description { Faker::Lorem.paragraph }
    image { Faker::Lorem.sentence }
    status { 'In' }
  end
end

Here is my test for controller Books action Index

require 'rails_helper'

RSpec.describe BooksController, type: :controller do
  before(:each) do
    @user = create(:user)
    sign_in @user
  end

  describe 'GET #index' do
    it 'assigns @books' do
      book = create(:book)
      get :index
      expect(assigns(:books)).to eq([book])
    end

    it 'render index template' do
      get :index
      expect(response).to render_template('index')
      expect(response).to have_http_status(200)
    end
  end
end

I have issue in this line expect(assigns(:books)).to eq([book]) . In this place test fails. I don't understand why, because my code on development environment works as expected. Also I made test by example in documentation. Here is console output of test:

Failures:

  1) BooksController GET #index assigns @books
     Failure/Error: expect(assigns(:books)).to eq([book])

       expected: [#<Book _id: 5af815723a4f8d22e7f3daed, name: "The Lathe of Heaven", author: "Edwin Runolfsdottir", de...t aut.", status: "In", image: "Nulla culpa sint perspiciatis nihil saepe perferendis quidem sint.">]
            got: [#<Book _id: 5af718cc3a4f8d176e551d86, name: "Precious Bane", author: "Aiden Dicki", description: "Op...luptatibus et sint soluta debitis saepe.", status: "In", image: "Aut et expedita placeat ut quos.">]

       (compared using ==)

       Diff:
       @@ -1,2 +1,6 @@
       -[#<Book _id: 5af815723a4f8d22e7f3daed, name: "The Lathe of Heaven", author: "Edwin Runolfsdottir", description: "Et dolorem tenetur et dolore. Sit et magni et ut quos quia. Ab expedita tenetur laborum cumque repellendus magnam. Perferendis saepe id cumque qui numquam. Non maxime et aut.", status: "In", image: "Nulla culpa sint perspiciatis nihil saepe perferendis quidem sint.">]
       +[#<Book _id: 5af718cc3a4f8d176e551d86, name: "Precious Bane", author: "Aiden Dicki", description: "Optio quisquam assumenda quaerat non et blanditiis ea. Fugit necessitatibus ipsum cupiditate. Suscipit explicabo vitae tempora illum omnis. Ipsum quam dolore. Rerum possimus ut vero non reprehenderit laboriosam.", status: "In", image: "Est sed autem molestiae earum.">,
       + #<Book _id: 5af718cc3a4f8d176e551d88, name: "Terrible Swift Sword", author: "Rey Beer", description: "Consequatur eligendi eaque aut quisquam voluptatum. Labore ad dolore expedita reiciendis repellat est. Error dolor et doloremque. Velit in qui fugit recusandae quia. Atque non ut accusantium consequatur similique.", status: "In", image: "Illum mollitia sed facere dolor quisquam nisi facilis.">,
       + #<Book _id: 5af718cc3a4f8d176e551d8a, name: "Bury My Heart at Wounded Knee", author: "Viva Leuschke PhD", description: "Voluptatum voluptatem laudantium possimus debitis suscipit velit. Eius assumenda dolorem. Praesentium officia provident. Officia soluta adipisci exercitationem aut at totam eveniet.", status: "In", image: "Ipsum qui nemo enim quo molestiae rerum fugit unde.">,
       + #<Book _id: 5af718cc3a4f8d176e551d8d, name: "Down to a Sunless Sea", author: "Tristian Breitenberg", description: "Ullam sunt repudiandae commodi aliquid repellendus est. Aut reprehenderit in magni saepe quis. Commodi incidunt sit. In repellat beatae aut et esse quae. Veniam nemo natus.", status: "Out", image: "Aut aut commodi quam et quis dolore non voluptates.">,
       + #<Book _id: 5af718cd3a4f8d176e551d90, name: "The Wives of Bath", author: "Devonte Mraz", description: "Similique enim neque et autem libero. Dolorem alias aut est veniam aperiam ea repellat. Consectetur iure quae nihil eos quo et. Voluptatibus et sint soluta debitis saepe.", status: "In", image: "Aut et expedita placeat ut quos.">]

Also I have tried to pass page as parameter, like this:

get(:index, params: { page: 1 })

But it didn't help. Please help me to solve this issue because all my other tests works fine. Tell me if you need more information. I can provide GitHub url. Thanks in advance.

It is an issue related with the database is not clean. As a quick fix try to add:

Book.destroy_all

Before creating a book. As you replied the configuration for database cleanner is a robust solution:

config.before(:suite) do 
  DatabaseCleaner[:mongoid].strategy = :truncation 
  DatabaseCleaner.clean_with(:truncation) 
end 

config.around(:each) do |example| 
  DatabaseCleaner.cleaning do 
    example.run 
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM