问题
I'm trying to write some tests (not for code coverage, but irrelevant here) in rspec for a ROR app and need to alias describe and it, at the least. I can alias describe just fine because it's at the top level. But, I can't get anything else to work. like this guy:
module RSpec
module Core
class ExampleGroupMethods
alias :they :it
end
end
end
I included that in the spec file but I am not getting the module path right. I looked over the rspec codebase but am hitting a wall so I don't think I quite know what I'm doing. Any tips or resources would be greatly appreciated.
回答1:
You want to use alias_example_to:
RSpec.configure do |c|
c.alias_example_to :they
end
This is part of the public API of RSpec.
回答2:
It looks like you want to use define_example_method. This is how RSpec defines the example group methods like it and specify which are all really just aliases of each other. Not sure if there's an approved API for tapping into it, but you should be able to do something like:
module RSpec
module Core
class ExampleGroupMethods
class << self
define_example_method :they
end
end
end
end
来源:https://stackoverflow.com/questions/12317558/alias-it-in-rspec