Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing with validates_comparison_of or comparison #1514

Closed
fabriciobonjorno opened this issue Oct 4, 2022 · 3 comments · Fixed by #1552
Closed

Testing with validates_comparison_of or comparison #1514

fabriciobonjorno opened this issue Oct 4, 2022 · 3 comments · Fixed by #1552

Comments

@fabriciobonjorno
Copy link

Guys, how are you? Has anyone implemented testing using these new Rails 7 validates? If yes, could you give me a hint?

First validate
validates :start_date, comparison: { greater_than_or_equal_to: Date.today }

Second validate
validates :end_date, comparison: { greater_than: :start_date }

Tutorial link:
https://www.bigbinary.com/blog/rails-7-adds-comparison-validator-to-active-record

@matsales28
Copy link
Member

Hello, I'm fine, thanks for asking!

I'd say that currently, shoulda-matchers does not have an option to test this new feature of Rails 7.
While we don't implement this on shoulda-matchers you can test this validation using this logic (just an example, you don't need to follow this pattern)

class Order
  validates :start_date, comparison: { greater_than_or_equal_to: Date.today }
  validates :end_date, comparison: { greater_than: :start_date }
end

RSpec.describe Order, type: :model do
  describe "validations" do
    context "when start date is less than the current day" do
      it "record is not valid and adds greater_than_or_equal_to error to start_date attribute" do
        reward = Order.new(start_date: Time.zone.tomorrow)

        expect(reward.valid?).to be false
        expect(reward.errors[:start_date]).not_to be_empty
      end
    end

    context "when start date is not less than the current day" do
      it "record is valid and doesn't add greater_than_or_equal_to error to start_date attribute" do
        reward = Order.new(start_date: Time.zone.tomorrow)

        expect(reward.valid?).to be true
        expect(reward.errors[:estimated_delivers_on]).to be_empty
      end
    end

    context "when end date is not greater than the start_date" do
      it "record is not valid and adds greater_than error to end_date attribute" do
        reward = Order.new(start_date: Time.zone.today, end_date: Time.zone.today)

        expect(reward.valid?).to be false
        expect(reward.errors[:start_date]).not_to be_empty
      end
    end

    context "when end date is greater than the start_date" do
      it "record is valid and doesn't add greater_than error to end_date attribute" do
        reward = Order.new(start_date: Time.zone.today, end_date: Time.zone.tomorrow)

        expect(reward.valid?).to be true
        expect(reward.errors[:start_date]).to be_empty
      end
    end
  end
end

I can probably work on implementing this in the next weeks, but we are happy to receive new contributions if you want to work on this 😄

@JuzerShakir
Copy link
Contributor

Any updates on this issue?

@matsales28
Copy link
Member

I started to work on a PR for implementing that feature but went on vacation and only had a little time to spend on that since. My plan is to work on that PR in the next weeks, starting this Friday. But feel free to implement that if you want to!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants