TABISH KHAN
TABISH KHAN

Reputation: 1623

RSPEC ERROR: expected: [2015-05-01 19:56:25 -0700] got: nil (compared using ==) Ruby on Rails

I am testing with ruby on rails (using rspec) - testing my events controller (specifically a variable assignment) like so:

Controller Code:

class EventsController < ApplicationController
 def index
   start_at = Time.zone.at(params[:start].to_f)
   puts start_at
 end

The puts start_at command above gives the output:

2015-05-01 19:56:25 -0700

RSPEC Test Code:

describe EventsController do
  login_user
  before do 
    @event = FactoryGirl.create(:event)
  end

    describe "INDEX action" do 
     it "assings correct start time" do 
      get('index', {:format=>:json, :start=> @event.starts_at,:end=>@event.ends_at})     
      expect(assigns(:start_at)).to eq(@event.starts_at)
     end
    end

Error Message:

   Failure/Error: expect(assigns(:start_at)).to eq(@event.starts_at)


   expected: Fri, 01 May 2015 19:56:25 PDT -07:00
        got: nil

   (compared using ==)

I am confused, why is it getting nil - when i output start_at it gives me a value. Therefore when i am testing, should it not return the same value as the output ?

Upvotes: 0

Views: 189

Answers (1)

infused
infused

Reputation: 24337

assigns is for testing instance variables, but you are trying to test a local variable. Use an instance variable in your controller instead:

@start_at = Time.zone.at(params[:start].to_f)

Upvotes: 1

Related Questions