RSpec test custom validator - ruby-on-rails

I have the following validator in my model:
class ContinuumValidator < ActiveModel::Validator
def validate(record)
if !record.end_time.nil? and record.end_time < record.start_time
record.errors[:base] << "An event can not be finished if it did not start yet..."
end
end
end
class Hrm::TimeEvent < ActiveRecord::Base
validates_with ContinuumValidator
end
How can I test it using Rspec?
Here is what I have tried so far: (thanks to zetetic)
describe "validation error" do
before do
#time_event = Hrm::TimeEvent.new(start_time: "2012-10-05 10:00:00", end_time: "2012-10-05 09:00:00", event_type: 2)
end
it "should not be valid if end time is lower than start time" do
#time_event.should_not be_valid
end
it "raises an error if end time is lower than start time" do
#time_event.errors.should include("An event can not be finished if it did not start yet...")
end
end
But I get the following errors:
1) Hrm::TimeEvent validation error raises an error if end time is lower than start time
Failure/Error: #time_event.errors.should include("An event can not be finished if it did not start yet...")
expected #<ActiveModel::Errors:0x007fd1d8e02c50 #base=#<Hrm::TimeEvent id: nil, start_time: "2012-10-05 08:00:00", end_time: "2012-10-05 07:00:00", event_type: 2, employee_id: nil, created_at: nil, updated_at: nil, not_punched: false, validated: false, replace_id: nil>, #messages={}> to include "An event can not be finished if it did not start yet..."
Diff:
## -1,2 +1,5 ##
-["An event can not be finished if it did not start yet..."]
+#<ActiveModel::Errors:0x007fd1d8e02c50
+ #base=
+ #<Hrm::TimeEvent id: nil, start_time: "2012-10-05 08:00:00", end_time: "2012-10-05 07:00:00", event_type: 2, employee_id: nil, created_at: nil, updated_at: nil, not_punched: false, validated: false, replace_id: nil>,
+ #messages={}>
What am I doing wrong? And how can I achieve my goal?
Any help or suggestion would be appreciated.
Thanks.

The problem is that you're expecting #time_event.errors to behave like an array of strings. It doesn't, it returns ActiveModel::Errors. As others pointed out, you also need to trigger the validations with a call to valid?:
it "raises an error if end time is lower than start time" do
#time_event.valid?
#time_event.errors.full_messages.should include("An event can not be finished if it did not start yet...")
end

This solution works for me (using Mongoid):
The model
class OpLog
...
field :from_status, type: String
field :to_status, type: String
...
validate :states_must_differ
def states_must_differ
if self.from_status == self.to_status
errors.add(:from_status, "must differ from 'to_status'")
errors.add(:to_status, "must differ from 'from_status'")
end
end
...
end
The test:
it 'is expected to have different states' do
expect { create(:oplog, from_status: 'created', to_status: 'created').to raise_error(Mongoid::Errors::Validations) }
end
So in your case I'd write a test like this (if using ActiveRecord):
it 'raises an error if end time is lower than start time' do
expect { create(Hrm::TimeEvent.new(start_time: "2012-10-05 10:00:00", end_time: "2012-10-05 09:00:00", event_type: 2)) }.to raise_error(ActiveRecord::Errors)
end

There are no errors because you haven't called an event that triggers the errors. This happens normally when a record is created or saved. You may not want to hit the database in your test though and then you can use the method valid? like this:
it "raises an error if end time is lower than start time" do
#time_event.valid?
#time_event.errors.should include("An event can not be finished if it did not start yet...")
end
Me personally would put these two tests into one since valid? is called in the first case.
Also a minor: if record.end_time is better than if !record.end_time.nil?. (In my opinion at least.... :-) )

I think the record wasnt validated therefore the validatior didn't run and no error was aded. You can see this in the code output. "validated: false"
try:
it "raises an error if end time is lower than start time" do
#time_event.valid?
#time_event.errors.should include("An event can not be finished if it did not start yet...")
end

You have not tested the validation actually, plus i would suggest you to make a single spec.
describe "validation error" do
before { #time_event = Hrm::TimeEvent.new(start_time: "2012-10-05 10:00:00", end_time: "2012-10-05 09:00:00", event_type: 2) }
it "raises an error if end time is lower than start time" do
#time_event.valid?
#time_event.errors.should include("An event can not be finished if it did not start yet...")
end
end
class ContinuumValidator < ActiveModel::Validator
def validate(record)
if record.end_time and record.end_time < record.start_time
record.error.add_to_base << "An event can not be finished if it did not start yet..."
end
end
end

Related

Rails Rspec allow multiple method call in one line

desc 'Remove credential state users who no longer request for confirm otp within 10 minutes'
task failed_user_cleaner: :environment do
puts "Daily UserRecord Cleaning CronJob started - #{Time.now}"
#user = User.with_state("credentials").with_last_otp_at(Time.now - 10.minutes)
Users::Delete.new(#user).destroy_all
puts "Daily UserRecord Cleaning CronJob ended - #{Time.now}"
end
Above is crop job rake file code.
then I've tried in many times and found in many times.
But I couldn't find the way to write unit test case for above job.
Help me to write test case correctly.
here is my spec code
require 'rails_helper'
describe 'users rake tasks' do
before do
Rake.application.rake_require 'tasks/users'
Rake::Task.define_task(:environment)
end
context 'when remove credential state users who no longer request for confirm otp within 10 minutes' do
let(:user) { create(:user, last_otp_at: Time.now - 11.minutes, state: "credentials") }
let (:run_users_rake_task) do
Rake.application.invoke_task 'users:failed_user_cleaner'
end
it 'calls right service method' do
#users = Users::Delete.new([user])
expect(#users).to receive(:destroy_all)
run_users_rake_task
end
end
end
here is the error log
Failures:
1) users rake tasks when remove credential state users who no longer request for confirm otp within 10 minutes calls right service method
Failure/Error: expect(#users).to receive(:destroy_all)
(#<Users::Delete:0x0000556dfcca3a40 #user=[#<User id: 181, uuid: nil, phone: "+66969597538", otp_secret: nil, last_otp_at: "2021-09-30 09:32:24.961548000 +0700", created_at: "2021-09-30 09:43:24.973818000 +0700", updated_at: "2021-09-30 09:43:24.973818000 +0700", email: nil, avatar: "https://dummyimage.com/300x300/f04720/153572.png?t...", refresh_token: "eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MzI5Njk4MDQsImV4c...", first_name_en: "Jenise", first_name_th: "Damion", last_name_en: "McCullough", last_name_th: "Beatty", nationality: "TH", thai_national_id: nil, thai_laser_code: nil, company_id: 200, role: nil, state: "credentials", date_of_birth: "2020-10-30 00:00:00.000000000 +0700", deleted_at: nil, password_digest: "$2a$04$jfR9X9ci06602tlAyLOoRewTK1lZ12vJ2cZ9Dc2ov4F...", username: "zreejme238", shopname: nil, access_token: nil, locked_at: nil, login_attempts: 0, locale: "th", scorm_completed: false>]>).destroy_all(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
# ./spec/tasks/users_spec.rb:19:in `block (3 levels) in <top (required)>'
You are creating two instances of Users::Delete when running this test, one within the test and one within the task. Since the instance within the test is not used, it is incorrect to expect it to receive a message.
Rspec has an expectation, expect_any_instance_of, that will fix this however consider reading the full page since it can create fragile or flaky tests. If you wanted to use this method, your test would look something like:
it 'calls right service method' do
expect_any_instance_of(Users::Delete).to receive(:destroy_all)
run_users_rake_task
end
Personally I'd instead check that the expected users were deleted with something like:
it 'removes the user' do
expect { run_users_rake_task }.to change { User.exists?(id: #user.id) }.to(false)
end
Unless you want to use any_instance_of (which is a code smell) you need to stub the Users::Delete method so that it returns a double and put the expectation on the double:
require 'rails_helper'
describe 'users rake tasks' do
before do
Rake.application.rake_require 'tasks/users'
Rake::Task.define_task(:environment)
end
context 'when remove credential state users who no longer request for confirm otp within 10 minutes' do
let(:user) { create(:user, last_otp_at: Time.now - 11.minutes, state: "credentials") }
let(:run_users_rake_task) do
Rake.application.invoke_task 'users:failed_user_cleaner'
end
let(:double) do
instance_double('Users::Delete')
end
before do
allow(Users::Delete).to receive(:new).and_return(double)
end
it 'calls right service method' do
expect(double).to receive(:destroy_all)
run_users_rake_task
end
end
end
However this really just tells us that the API of the service object is clunky and that you should write a class method which both instanciates and performs:
module Users
class Delete
# ...
def self.destroy_all(users)
new(users).destroy_all
end
end
end
desc 'Remove credential state users who no longer request for confirm otp within 10 minutes'
#...
Users::Delete.destroy_all(#user)
# ...
end
require 'rails_helper'
describe 'users rake tasks' do
# ...
context 'when remove credential state users who no longer request for confirm otp within 10 minutes' do
# ...
it 'calls right service method' do
expect(Users::Delete).to receive(:destroy_all)
run_users_rake_task
end
end
end

comparison of String with ActiveSupport::TimeWithZone failed

My application gives users the opportunity to create events with a start and end time:
>> Event.where('end_time < ?', 1.days.ago).first
Event Load (0.3ms) SELECT "events".* FROM "events" WHERE (end_time < '2017-12-21 13:30:36.411209') ORDER BY "events"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<Event id: 1, user_id: 1, place: "london", start_time: "2017-08-26 20:00", end_time: "2017-08-26 22:00", note: "Note", note_id: 355, picture: "dwarf_hammering.jpg", created_at: "2017-08-14 11:27:16", updated_at: "2017-08-14 11:27:16">
As you can see the start_time and end_time are String with a pre-defined format, and Active Record is able to compare them against a Time instance.
I created a Sidekiq job to delete all events that are older than one day:
class EventsCleanerWorker
include Sidekiq::Worker
def perform
events = Event.where('end_time < ?', 1.days.ago)
if events.any?
events.delete_all
end
end
end
Inside test/fixtures/events.yml I created 6 events: two of them have an end_time older than one day, as the event below:
event3:
user: John
place: "London"
start_time: 3.days.ago.strftime("%Y-%m-%d %H:%M")
end_time: 2.days.ago.strftime("%Y-%m-%d %H:%M")
note: "A thrilling adventure"
Then I created the following test:
def test_events_cleaner_worker_inline_mode
assert Event.count == 6
assert events(:event3).end_time < 1.days.ago
EventsCleanerWorker.perform_async
assert Event.all.reload.count == 4
assert_equal 0, EventsCleanerWorker.jobs.size
end
However, when I run this test I receive the following error:
ArgumentError: comparison of String with ActiveSupport::TimeWithZone failed
This error is referred to assert events(:event3).end_time < 1.days.ago
If I remove this line the test fails because the test expects Event.all.reload.count to be 6 instead of 4. I have no idea why the string generated by end_time inside the events fixtures cannot be compared against Time, while this is possible in the dev environment.
start_time and end_time in the fixture should be embed in ERB as follows:
event3:
user: John
place: "London"
start_time: <%= 3.days.ago.strftime("%Y-%m-%d %H:%M") %>
end_time: <%= 2.days.ago.strftime("%Y-%m-%d %H:%M") %>
note: "A thrilling adventure"

Rspec: test if method was called on an object inside a job?

I made a job to call a method if ended_at was less than today. For some reason I keep getting failures even if I throw a binding pry right at the moment it is about to break and manually call the method and it works fine. However, I still get a failure if I just let the spec test run on its own.
My job:
class RemoveOldEventsFromAlgoliaJob < ApplicationJob
queue_as :default
def perform
old_events = Event.where("ended_at < ?", Date.today)
old_events.each do |e|
e.remove_from_algolia
end
end
end
My method:
def remove_from_algolia
index = Algolia::Index.new(##ALGOLIA_INDEX_NAME)
index.delete_object(self.id)
end
My spec test:
require 'rails_helper'
RSpec.describe RemoveOldEventsFromAlgoliaJob, type: :job do
it "will remove old events from the index" do
ActiveJob::Base.queue_adapter = :test
event = FactoryGirl.create(:event, title: "EXPIRED EVENT", ended_at: 1.day.ago)
RemoveOldEventsFromAlgoliaJob.perform_now
expect(event).to receive(:remove_from_algolia)
end
end
RSpec output:
Failures:
1) d will remove old events from the index
Failure/Error: expect(event).to receive(:remove_from_algolia)
(#<Event id: 401, uuid: "6e9a6f08-c34d-45af-9f3c-870b28643809", organization_id: nil, event_type_id: nil, name: "cool-event", title: "Cool Event", description: "Rad thing that's gonna happen", platform_type: "ee", platform_id: "12345678", platform_url: "http://event.com/12345678", featured: false, capacity: 100, rsvp_count: nil, attendee_count: nil, status: "upcoming", started_at: "2017-10-21 03:00:39", ended_at: "2017-03-23 07:00:00", deleted_at: "2017-10-21 03:00:39", created_at: "2017-03-24 00:24:54", updated_at: "2017-03-24 00:24:54", location_line_1: nil, location_line_2: nil, location_city: nil, location_state: nil, location_zip: nil, location_country: nil>).remove_from_algolia(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
# ./spec/jobs/remove_old_events_from_algolia_job_spec.rb:10:in `block (2 levels) in <top (required)>'
Finished in 0.8828 seconds (files took 7.12 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/jobs/remove_old_events_from_algolia_job_spec.rb:4 # RemoveOldEventsFromAlgoliaJob will remove old events from the index
Coverage report generated for RSpec to /workproject/coverage. 384 / 496 LOC (77.42%) covered.
I can put a binding.pry inside my job during the spec test and call that method successfully:
4: def perform
5:
6: old_events = Event.where("ended_at < ?", Date.today)
7: old_events.each do |e|
=> 8: binding.pry
9: e.remove_from_algolia
10: end
11: end
[1] pry(#<RemoveOldEventsFromAlgoliaJob>)> e.remove_from_algolia
=> {"deletedAt"=>"2017-03-24T00:37:53.743Z", "taskID"=>206043962, "objectID"=>"409"}
[2] pry(#<RemoveOldEventsFromAlgoliaJob>)>
Try using a stub for event instead of a real Event model:
require 'rails_helper'
RSpec.describe RemoveOldEventsFromAlgoliaJob, type: :job do
it "will remove old events from the index" do
ActiveJob::Base.queue_adapter = :test
event = double('event')
allow(Event).to receive(:where).and_return([event])
expect(event).to receive(:remove_from_algolia)
RemoveOldEventsFromAlgoliaJob.perform_now
end
end

model method trigger boolean to true?

How can I trigger method accomplished_challenge upon days_left_challenged == 0?
challege.rb
before_save :days_left_challenged_sets_deadline
# makes ongoing_challenge become past (before_save) THIS WORKS
def days_left_challenged_sets_deadline
if self.date_started != nil
if days_left_challenged <= 0
self.accomplished = true
self.deadline = self.date_started
end
end
end
# makes ongoing_challenge become past (whenever gem) THIS DOESN'T
def self.accomplished_challenge
self.all.each do |challenge|
if challenge.days_left_challenged <= 0
challenge.accomplished = true
challenge.deadline = self.date_started
end
end
end
# Counts down how many days left in days_challenged using committed
def days_left_challenged
self.days_challenged - ((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end + self.missed_days
end
Challenge.last
id: 1,
action: "Run",
committed: ["sun", "mon", "tue", "wed", "thu", "fri", "sat", ""],
date_started: Sat, 06 Feb 2016 00:00:00 EST -05:00,
deadline: nil,
accomplished: nil,
days_challenged: 10,
missed_days: 0,
I can't trigger it with a callback or validation I don't think since days_left_challenged can turn to 0 at any point in the life of a challenge.
I suggest you use a gem like Whenever to setup a cron job to run every day or so and do that checking for all Challenges. It would be something like:
every 1.day, :at => '0:01 am' do
runner "Challenge.accomplished_challenge"
end
And your accomplished_challenge must be a class method that checks all (or the one you choose using a filter) Challenges:
def self.accomplished_challenge
self.all.each do |challenge|
if challenge.days_left_challenged == 0
challenge.update_attributes(deadline: self.date_started, accomplished: true)
end
end
end
---- EDIT to work on Heroku ----
Create a task on /lib/tasks/scheduler.rake:
# /lib/tasks/scheduler.rake
desc "This task is called by the Heroku scheduler add-on"
task :check_accomplished_challenges => :environment do
puts "Checking accomplished challenges..."
Challenge.accomplished_challenge
puts "done."
end
Go to your heroku app Resources page and add 'Heroku Scheduler'. Open the scheduler and add the task:
rake check_accomplished_challenges
Set it to run every day.
More details: https://devcenter.heroku.com/articles/scheduler

Why am I getting a "SystemStackError: stack level too deep" in my rails3 beta4 model

I'm getting the following error: SystemStackError: stack level too deep when executing the following code in rails3 beta4 under ruby 1.9.2-rc1:
ruby-1.9.2-rc1 > f = Forum.all.first
=> #<Forum id: 1, title: "Forum 1", description: "Description 1", content: "Content 1", parent_id: nil, user_id: 1, forum_type: "forum", created_at: "2010-07-17 04:39:41", updated_at: "2010-07-17 04:39:41", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil>
ruby-1.9.2-rc1 > f.children
=> [#<Forum id: 2, title: "Thread 2", description: "Description 2", content: "Content 2", parent_id: 1, user_id: 1, forum_type: "thread", created_at: "2010-07-17 04:40:17", updated_at: "2010-07-17 04:40:17", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil>]
ruby-1.9.2-rc1 > f.forum_type = "thread"
=> "thread"
ruby-1.9.2-rc1 > f.save
SystemStackError: stack level too deep
from /Users/emilkampp/.rvm/rubies/ruby-1.9.2-rc1/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!!
ruby-1.9.2-rc1 >
And that is caused by the following code:
# Before and after filters
#
before_update :update_all_nested_objects, :if => :forum_type_changed?
protected
# Checks if the +forum_type+ has been changed
#
def forum_type_changed?
self.forum_type_changed?
end
# Updates all nested upjects if the +forum_type+ has been changed
#
# This will trigger the +update_all_nested_objects+ method on all touched children, thus
# starting a chain-reaction all the way through the forum-tree.
#
# NOTE: This is where the error is triggered, since this is the only non-tested looping code, and my test-
# cases hasn't changed since this was added.
#
def update_all_nested_objects
children.each do |child|
child.forum_type = child_type
child.save
end
end
So, what's going on. I have been checking around a little, but nobody seems to have the same problem. Either it's not the same ruby version, thus the workflow.rb file is different, or the stack level to deep is caused by something else.
Any help will be greatly apreciated!
Best regards
// Emil
You are calling same method in method itself
def forum_type_changed?
self.forum_type_changed? #This will never ending process hence it gives error
end
I think you have same method name and column name that causing problem change your method name then
def check_forum_type_changed?
self.forum_type_changed? #method name and column name are different
end

Resources