问题描述
我已经阅读了一些关于如何组织 rspec 代码的内容.似乎上下文"更多地用于对象的状态.用您的话来说,您会如何描述如何在 rspec 代码中使用describe"?
I've read a little bit about how one should organize rspec code. It seems like "context" is used more for states of objects. In your words, how would you describe how to use "describe" in rspec code?
这是我的 movie_spec.rb 代码片段:
Here is a snippet of my movie_spec.rb code:
require_relative 'movie'
describe Movie do
before do
@initial_rank = 10
@movie = Movie.new("goonies", @initial_rank)
end
it "has a capitalied title" do
expect(@movie.title) == "Goonies"
end
it "has a string representation" do
expect(@movie.to_s).to eq("Goonies has a rank of 10")
end
it "decreases rank by 1 when given a thumbs down" do
@movie.thumbs_down
expect(@movie.rank).to eq(@initial_rank - 1)
end
it "increases rank by 1 when given a thumbs up" do
@movie.thumbs_up
expect(@movie.rank).to eq(@initial_rank + 1)
end
context "created with a default rank" do
before do
@movie = Movie.new("goonies")
end
it "has a rank of 0" do
expect(@movie.rank).to eq(5)
end
end
推荐答案
describe
和 context
没有太大区别.区别在于可读性.当我想根据条件分离规范时,我倾向于使用 context
.我使用 describe
来分隔被测试的方法或被测试的行为.
There is not much difference between describe
and context
. The difference lies in readability. I tend to use context
when I want to separate specs based on conditions. I use describe
to separate methods being tested or behavior being tested.
最新 RSpec 中的一个主要变化是 "context" 不能再用作顶级方法.
One main thing that changed in the latest RSpec is that "context" can no longer be used as a top-level method.
这篇关于在 rspec 中描述与上下文.差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!