mardi 3 mars 2015

undefined method `expect' for DateTime:Class when trying to mock datetime in Minitest ruby

I'm implementing an last_active_at attribute on the User model of an application, which will be updated every time a user sees a page on the website.


When trying to test that the attribute is right in my test suite with a mock on the datetime, I get a NoMethodError: undefined method 'expect' for DateTime:Class.


Here is my test in the file chapter_controller_test.rb :



test "save user last activity timestamp" do
user = FactoryGirl.create(:user, student_type: User::REMOTE)
session[:user_id] = user.id
some_date = DateTime.new(2014, 12, 12, 1, 1, 1)
DateTime.expect(:now, some_date)
get :index
assert_equal(some_date, user.last_active_at)
end


The schema for the user table :



create_table "users", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "admin", default: false
t.string "email"
t.string "password_digest"
t.string "reset_password_key"
t.string "student_type", default: "remote"
t.datetime "last_active_at"
end


And the implementation of the activity record in ApplicationController :



before_filter :record_activity

def record_activity
if current_user && current_user.remote?
current_user.last_active_at = DateTime.now
current_user.save
end
end


I'm using minitest 5.1


Thanks !


Aucun commentaire:

Enregistrer un commentaire