-
-
Notifications
You must be signed in to change notification settings - Fork 766
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
Custom helper method with hooks #1378
Comments
I think this'll do what you want: module PaperTrail
module RSpec
module ExampleMethods
# :call-seq:
# with_versioning
#
# enable versioning for specific blocks
def with_versioning
was_enabled = ::PaperTrail.enabled?
::PaperTrail.enabled = true
begin
yield
ensure
::PaperTrail.enabled = was_enabled
end
end
end
module Macros
def with_versioning(&block)
context "with versioning" do
around(:each) { |ex| with_versioning(&ex) }
class_exec(&block)
end
end
end
end
end
RSpec.configure do |rspec|
rspec.include PaperTrail::RSpec::ExampleMethods
rspec.extend PaperTrail::RSpec::Macros
end
describe "whatever" do
it "does x" do
end
with_versioning do
before(:each) { do_something }
it "does y" do
end
end
end ...then I think it would be confusing for the before hook within
|
The macro version could also just create a context with the metadata automatically set, rather than adding an additional hook. |
@myronmarston - Cheers! I was able to get it to work by using most of your suggestions and doing something very similar. The key was the I wasn't able to get the Here's what I ended up doing for the implementation if you're curious. You are correct, it was necessary to wrap the contents of the block received inside of a |
|
This is not an issue, but a question that I've been trying to wrap my head around and have not found a good answer to yet. If this is documented or this is not the proper forum for this type of thing then please point me in the right direction as to where I should go.
Please see paper-trail-gem/paper_trail#312 for some background on what I'm trying to accomplish. The idea here is that I want to add a helper method which has the ability to hook into the run-sequence for the test contents of the block within it.
We have an RSpec helper that adds a
before(:each)
hook to the RSpec configuration which sets the default value for a method that is essentially anattr_accessor
field. You can view this code here. There is a custom metadata,:versioning => true
that can be attached, which hooks into thebefore
sequence, and reverses the value of that boolean. There is also a custom helper method,with_versioning
, that Iinclude
, (and in retrospect should probably beextending
as well) into the RSpec configuration. Here is the code for the custom helper method. Basically, I want to be able to do something like this with my RSpec syntax (pretend thewith_versioning
method is extended as a class method to RSpec's configuration):I've tried switching the syntax of the
with_versioning
helper method in the hopes that I might be able to get it to insertbefore(:each)
hooks on the fly (ordescribe '', versioning: true
), but that hasn't worked. Is there a way to get what I'm trying to accomplish here to work? Any help / suggestions would be much appreciated.The text was updated successfully, but these errors were encountered: