在rspec中描述vs context。区别在哪里?

时间:2014-10-21 01:16:37

标签: ruby rspec describe

我已经读过一些关于如何组织rspec代码的内容。好像" context"更多地用于对象的状态。用你的话来说,你将如何描述如何使用"描述"在rspec代码?

以下是我的movie_spec.rb代码的片段:

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

1 个答案:

答案 0 :(得分:16)

describecontext之间没有太大区别。不同之处在于可读性。当我想根据条件分离规范时,我倾向于使用context。我使用describe来分隔正在测试的方法或正在测试的行为。

最新的RSpec改变的一个主要问题是"context" can no longer be used as a top-level method