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

Monkey patch to allow use of validation_scopes #779

Closed
samstickland opened this issue Sep 9, 2015 · 5 comments
Closed

Monkey patch to allow use of validation_scopes #779

samstickland opened this issue Sep 9, 2015 · 5 comments

Comments

@samstickland
Copy link

Hi,

We use https://github.com/gtd/validation_scopes quite heavily and it would be useful to be able to use these matchers on the validation scopes it generates.

I initially thought that it might require quite some work, but I eventually settled on this which seems to do the job:

module ValidationScopesHelpers
  def rewire_errors_to_warnings(object)
    def object.valid?
      has_warnings?
    end

    def object.errors
      warnings
    end
  end
end

Here's an example. Say we want to test the validation in this ActiveRecord object

class Obj < ActiveRecord::Base
  validates :name, presence: true

  validation_scope :warnings do |o|
    o.validates :address, presence: true
  end
end

The test case would look like this:

include ValidationScopesHelpers
subject(:obj) { FactoryGirl.build :obj }

context "regular validation tests" do
  it { is_expected.to validate_presence_of(:name) }
end

context "validation warning tests" do
  before { rewire_errors_to_warnings(obj) }

  it { is_expected.to validate_presence_of(:address) }
end

Can anyone see any downsides with this approach?

@mcmire
Copy link
Collaborator

mcmire commented Sep 22, 2015

Sorry for the late reply on this. While generally I don't like overriding core ActiveRecord methods, I don't see this as particularly egregious, since this is only relegated to the tests, and only done on demand.

Ideally, the matchers would expose some kind of option that would let you override the strategy for validating records and checking whether they're valid, but this would require some refactoring all over the place, so I'm not sure that's worth it.

@mcmire
Copy link
Collaborator

mcmire commented Sep 23, 2015

So I don't think this is something we can change in shoulda-matchers, but using that monkey patch in your own tests seems reasonable to me.

@mcmire mcmire closed this as completed Sep 23, 2015
@samstickland
Copy link
Author

OK cool :)

I can make this into a wiki page if you like?

2015-09-23 23:45 GMT+01:00 Elliot Winkler [email protected]:

So I don't think this is something we can change in shoulda-matchers, but
using that monkey patch in your own tests seems reasonable to me.


Reply to this email directly or view it on GitHub
#779 (comment)
.

@mcmire
Copy link
Collaborator

mcmire commented Sep 24, 2015

@samstickland Sure! I just created a wiki and added a section to the home page. Create a new page that mentions this and then add it to the home page.

@samstickland
Copy link
Author

Done!

2015-09-24 15:31 GMT+01:00 Elliot Winkler [email protected]:

@samstickland https://github.com/samstickland Sure! I just created a
wiki and added a section to the home page. Create a new page that mentions
this and then add it to the home page.


Reply to this email directly or view it on GitHub
#779 (comment)
.

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

No branches or pull requests

2 participants