ruby 如何在 RSPEC 中测试类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8004591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test class methods in RSPEC
提问by lulalala
I wrote a simple class method Buy.get_days(string), and is trying to test it with different text string inputs. However I feel it is very verbose.
我写了一个简单的类方法Buy.get_days(string),并试图用不同的文本字符串输入来测试它。但是我觉得它很冗长。
- Is there any more concise way to test the following?
- Is there a
equivalent of
subjectfor methodswhich I can just keep passing different parameters in and check the results? - Is there a way to avoid the unnecessary description at each
it?
- 有没有更简洁的方法来测试以下内容?
- 是否有等效的
subjectfor方法,我可以继续传递不同的参数并检查结果? - 有没有办法避免不必要的描述
it?
thanks
谢谢
describe Buy do
describe '.get_days' do
it 'should get days' do
Buy.get_days('Includes a 1-weeknight stay for up to 4 people')
.should == 1
end
it 'should get days' do
Buy.get_days('Includes a 1-night stay in a King Studio Room with stone fireplace')
.should == 1
end
it 'should get days' do
Buy.get_days('Includes 4 nights/5 days at the Finisterra Hotel for up to two adults and two children (staying in the same room)')
.should == 4
end
end
end
采纳答案by TCopple
回答by Matt Sanders
There isn't a subjectequivalent for calling a method, so using itis the way to go here. The issue I see with your code as presented is that it doesn't actually explain whatyou are testing for. I would write something more like:
没有subject调用方法的等价物,因此使用it是这里的方法。我看到你的代码所呈现的问题是,它实际上并没有解释什么,你是为测试。我会写一些更像:
describe Buy do
describe '.get_days' do
it 'should detect hyphenated weeknights' do
Buy.get_days('Includes a 1-weeknight stay for up to 4 people').should == 1
end
it 'should detect hyphenated nights' do
Buy.get_days('Includes a 1-night stay in a King Studio Room with stone fireplace').should == 1
end
it 'should detect first number' do
Buy.get_days('Includes 4 nights/5 days at the Finisterra Hotel for up to two adults and two children (staying in the same room)').should == 4
end
end
end
I'm making assumptions about what you're after here, but hopefully the idea is clear. This will also lead to much more helpful error output when a test fails. Hope this helps!
我正在对你在这里追求的东西做出假设,但希望这个想法很清楚。当测试失败时,这也将导致更有用的错误输出。希望这可以帮助!
回答by ahnbizcad
Apparently there is a described_classmethod.
显然是有described_class方法的。
https://www.relishapp.com/rspec/rspec-core/docs/metadata/described-class
https://www.relishapp.com/rspec/rspec-core/docs/metadata/written-class
I suppose it's cleaner than subject.class, since it doesn't introduce another .method call, which reduces readability.
我想它比 更干净subject.class,因为它没有引入另一个.方法调用,这会降低可读性。
Using either described_classor subject.classmay be more DRY than mentioning the class explicitly in every example. But personally I think not getting the syntax highlighting that comes with mentioning the class name explicitly is kind of a bummer, and I think it reduces readability, despite it totally winning in the maintainability department.
使用described_class或subject.class可能比在每个示例中明确提及该类更枯燥。但我个人认为,没有明确提及类名所带来的语法高亮是一种无赖,而且我认为它降低了可读性,尽管它在可维护性部门完全获胜。
A question arises regarding best practice:
关于最佳实践的问题出现了:
Should you use described_class whenever possible inside and outside the .expect()method, or only within the expect()method?
你应该尽可能在.expect()方法内部和外部使用描述类,还是只在expect()方法内部使用?
回答by Omar Ali
This might be an old question but you can always use subject.classto get by:
这可能是一个老问题,但您始终subject.class可以通过以下方式获得:
describe Buy do
describe '.get_days' do
it { expect(subject.class.get_days('Includes a 1-weeknight stay for up to 4 people')).to eq 1 }
end
end
回答by bjnord
An alternative to using subject/itis to use before/specify:
使用subject/的替代方法it是使用before/ specify:
describe '#destroy' do
context 'with children' do
before { @parent = FactoryGirl.create(:parent, children: FactoryGirl.create_list(:child, 2) }
specify { @parent.destroy.should be_false }
end
end
This will produce a reasonable description in RSpec's -fdoutput format:
这将在 RSpec 的-fd输出格式中产生合理的描述:
#destroy
with children
should be false

