在我的Ruby库中,我有一些旨在通过继承来使用的类。 因此我在spec文件中做的第一件事是定义一个定制的Foo类:
module MyGem describe MyClass do class FooMyClass < MyClass ... end end end问题是定义的类将泄漏到其他测试,并且必须小心使用唯一名称或使用after :all块删除类。
考虑到Rspec已经提供的所有魔法,这感觉有点手动。 有没有更好的方法来定义抽象类的规格? 大多数情况下,我想要一个简单的方法来清理所有临时声明的命名空间。
当我定义像这样的多个类时,特别讨厌清理:
module MyGem describe MyClass do ... end class FooMyClass < MyClass ... end describe FooMyClass do ... end end这更难以正确地用after :all或after :each定义after :each块。
In my Ruby library I have some classes that are intended to be used through inheritance. Therefore the first thing I do in my spec file is defining a custom Foo class:
module MyGem describe MyClass do class FooMyClass < MyClass ... end end endThe problem is that the defined class will leak to other tests and must be careful using unique names or removing the class with an after :all block.
This feels a little bit manual considering all the magic already given by Rspec. Is there a better way to define specs for abstract classes? Mostly I want an easy way to clean the namespace of all temporal declarations.
It is specially annoying to clean up when I define multiple classes like this:
module MyGem describe MyClass do ... end class FooMyClass < MyClass ... end describe FooMyClass do ... end endThis is even harder to properly un-define with after :all or after :each blocks.
最满意答案
一种可能是使用匿名类 。
let(:fooMyClass) do Class.new(MyClass) do # ... end end这样就没有必要做清理了。
One possibility is to use anonymous classes.
let(:fooMyClass) do Class.new(MyClass) do # ... end endThis way there is no need to do clean up.
更多推荐
发布评论