alias 'it' in rspec

别来无恙 提交于 2019-11-29 12:54:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!