RSpec: describe, context, feature, scenario?

前端 未结 3 1920
广开言路
广开言路 2020-12-22 21:04

describe, context, feature, scenario: What is the difference(s) among the four and when do I use each one?

3条回答
  •  难免孤独
    2020-12-22 21:13

    Expanding on Pierre's excellent answer, according to the docs:

    The feature and scenario DSL correspond to describe and it, respectively. These methods are simply aliases that allow feature specs to read more as customer and acceptance tests.

    So for those familiar with the Mocha terms describe and it (which are better suited to describing the behaviour of a test from a user's perspective, hence Mocha primarily functioning as a front end testing framework), you could:

    • choose to always and only use describe and it or another pairing
    • choose to use it inside of a context block that requires multiple assertions/tests to be made in a specific app state

    Going with the second option, you can still follow the intention of "...wrap[ping] a set of tests against one functionality under the same state".

    Thus your tests might look like this:

    #
    # The feature/behaviour I'm currently testing
    #
    describe "item ordering" do
    
      # 1st state of the feature/behaviour I'm testing
      context "without an order param" do
        # 1st and only test we want to run in this state
        it "asks the user for missing order param" do
         ...
        end
      end
    
      # 2nd state of the feature/behaviour I'm testing
      context "with an invalid order param" do
        # 1st test we want to run in this state
        it "validates and rejects order param" do
          ...
        end
        # 2nd test we want to run in this state
        it "returns an error to user" do
          ...
        end
      end
    
      # 3rd state of the feature/behaviour I'm testing with multiple tests
      context "with a valid order param" do
        it "validates and accepts order param" do
          ...
        end
        it "displays correct price for order" do
          ...
        end
        unless being_audited
          it "secretly charges higher price to user" do
            ...
          end
        end
      end
    end
    

    This way you skip the feature keyword entirely, which you might want to use for specific front end features or if you are doing FDD (feature driven development), which may feel uncomfortable for some. Ask your developer team for input here.

    Caveat: don't always follow industry standards, imagine if we modelled all our tests after the Volkswagen philosophy?

提交回复
热议问题