zoukankan      html  css  js  c++  java
  • rspec中的shared_examples与shared_context有什么不同

    What is the real difference between shared_examples and shared_context ?

    My observations :

    1. I can test same things using both (i.e. with shared_examples or shared_context)

    2. But some of my other tests fails if I use later one.

    Observation #1 :

    I compared shared_examples and shared_context per documentation onhttps://www.relishapp.com/

    Syntactical differences are :

    • shared_context to define a block that will be evaluated in the context of example groups by implicitly matching metadata

    Example :

    shared_context "shared stuff", :a => :b do
      ...
    end
    • The way these are included or called from a test file

    shared_examples

    include_examples "name"      # include the examples in the current context
    it_behaves_like "name"       # include the examples in a nested context
    it_should_behave_like "name" # include the examples in a nested context

    shared_context

    include_context "shared stuff"

    Observation #2

    I have a test case

    shared_context 'limit_articles' do |factory_name|
      before do
        @account = create(:account)
      end
    
      it 'should restrict 3rd article' do
        create_list(factory_name, 3, account: @account)
    
        article4 = build(factory_name, account: @account)
        article4.should be_invalid
      end
    
      it 'should allow 1st article' do
        ...
      end
    
      it 'should allow 2nd article' do
        ...
      end
    end

    And include the context in a spec file which already has one shared_context included, then the existing one fails. But I change the order then all my test passes

    Fails

    include_context 'existing_shared_context'
    
    include_context 'limit_articles'

    Also if I replace the shared_context with shared_examples and accordingly include it in test case.

    Passes

    include_context 'existing_shared_context'
    
    it_behaves_like 'limit_articles'

    原文:http://stackoverflow.com/questions/21117123/rspec-shared-examples-vs-shared-context
  • 相关阅读:
    webService总结(一)——使用CXF公布和调用webService(不使用Spring)
    男人最佳的生育年限,程序猿们,看看吧!!!
    软考之路(5)——计算机组成原理之加密技术和认证技术
    新安装mysql 第三方工具连接不上问题
    JQuery text()、html() 以及 val()
    DOM
    Spring Boot 学习
    JSON
    Nodejs 配置+基础
    配置-
  • 原文地址:https://www.cnblogs.com/goody9807/p/5481641.html
Copyright © 2011-2022 走看看