timecop.travel test returns false instead of true - rails

293 views Asked by At

I am writing a unit test to check whether 24 hours have passed. If 24 hours have passed then it should return true

here is my attempt

test   "messenger tag is more than 24 hours" do
        
        Timecop.travel 2.days.ago
        account = accounts(:messenger_v2)
        contact_d = Contact.create! account: account, name: 'Mr Right', phone_number: nil, external_id: '155581474881005', contact_type: 'MessengerV2', source: 'Inbound', is_registered: true, primary_contact: true
        conversation = Conversation.create! contact: contact_d, account: account, status: 'Open', unread: true, conversation_type: 'Private'
        Message.create! contact: contact_d, message_type: 'Text', text: 'I have some enquries', direction: 'IN', account: account, conversation: conversation, external_id: "in_a#{Time.now.to_i.to_s}"
        msg = conversation.messages.incoming
        time_created = msg.last.created_at
        messenger_tags = time_created < 24.hours.ago
        assert_equal true, messenger_tags
            
    end

when I run the test here is the output

test_messenger_tag_is_more_than_24_hours                        FAIL (0.14s)
        Expected: true
          Actual: false
        test/models/message_test.rb:175:in `block in <class:MessageTest>'

kindly assist

1

There are 1 answers

4
Sampat Badhe On BEST ANSWER

you need to turn off Timecop using Timecop.returnonce the message is created.

test "messenger tag is more than 24 hours" do
    Timecop.travel 2.days.ago
    account = accounts(:messenger_v2)
    contact_d = Contact.create! account: account, name: 'Mr Right', phone_number: nil, external_id: '155581474881005', contact_type: 'MessengerV2', source: 'Inbound', is_registered: true, primary_contact: true
    conversation = Conversation.create! contact: contact_d, account: account, status: 'Open', unread: true, conversation_type: 'Private'
    Message.create! contact: contact_d, message_type: 'Text', text: 'I have some enquries', direction: 'IN', account: account, conversation: conversation, external_id: "in_a#{Time.now.to_i.to_s}"
    Timecop.return
    msg = conversation.messages.incoming
    time_created = msg.last.created_at
    messenger_tags = time_created < 24.hours.ago
    assert_equal true, messenger_tags
end

Update Or you can block to Timecop.travel, to avoid calling Timecop.return mentioned by @Stefan in the below comment

test "messenger tag is more than 24 hours" do
    Timecop.travel 2.days.ago do
        account = accounts(:messenger_v2)
        contact_d = Contact.create! account: account, name: 'Mr Right', phone_number: nil, external_id: '155581474881005', contact_type: 'MessengerV2', source: 'Inbound', is_registered: true, primary_contact: true
        conversation = Conversation.create! contact: contact_d, account: account, status: 'Open', unread: true, conversation_type: 'Private'
        Message.create! contact: contact_d, message_type: 'Text', text: 'I have some enquries', direction: 'IN', account: account, conversation: conversation, external_id: "in_a#{Time.now.to_i.to_s}"
        msg = conversation.messages.incoming
        @time_created = msg.last.created_at
    end
    messenger_tags = @time_created < 24.hours.ago
    assert_equal true, messenger_tags
end