ruby on rails - Rspec test got: nil -


i'm trying basic rspec test, looks this;

require 'rails_helper'    describe reviewscontroller    describe "get #index"    "assigns new review @reviews"    review = review.create( rating: 4 )     :index     expect(assigns(:review)).to eq([review])    assert_response :success   end  end end 

but i'm getting failure: expected: review id: 8, rating: 4, created_at: "2016-07-19 11:58:28", updated_at: "2016-07-19 11:58:28", user_id: nil, game_id: nil got: nil

my reviewscontroller looks this:

class reviewscontroller < applicationcontroller    def index    @reviews = review.all   end    def show    @review = review.find(params[:rating])   end    def create    review = review.new(review_params)     respond_to |format|    if @review.save     format.html { redirect_to root_url, notice: 'review updated.' }     format.json { render :show, status: :ok, location: @review }   else     format.html { render :new }     format.json { render json: @review.errors, status: :unprocessable_entity }    end   end  end    private    def post_params    params.require(:post).permit(:message)   end  end 

in case need it, here's review model:

class review < activerecord::base  belongs_to :user  belongs_to :game  validates_presence_of :rating   validates_uniqueness_of :user_id end 

i don't understand why it's asking user_id or game_id, because it's reviews..

you have change below

expect(assigns(:review)).to eq([review]) 

to

expect(assigns(:reviews)).to eq([review]) 

the reason @reviews instance variable have inside #index controller action, not @review.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -