代码之家  ›  专栏  ›  技术社区  ›  nimmolo

有没有一种方法可以为Capybara会话编写Minitest“DSL”模块(类似于rails-dom测试)

  •  0
  • nimmolo  · 技术社区  · 3 年前

    对于集成测试 Rails docs 提出了一种有趣的模式来编写“DSL”模块,该模块可以“使用断言方法扩展[会话]实例”,以实现特定于某些类型会话的断言模式。

    (…好吧,在这里使用术语“DSL”似乎有些牵强,但那是Rails文档。)

    他们的例子,使用 rails-dom-testing 会话,是这样的:

    require("test_helper")
    
    class StudentTest < IntegrationTestCase
    
      def test_creating_drafts
        rolf_session    = open_session.extend(AdminDsl)
        mary_session    = open_session.extend(CreatorDsl)
        ...open some other sessions
    
        rolf_session.login!(rolf)
        mary_session.login!(mary)
    
        url = mary_session.create_paper(name, gen_desc, project)
        rolf_session.check_admin(url, gen_desc, project)
        ... keep switching between sessions and do stuff
      end
    
      module AdminDsl
        def check_admin(url, gen_desc, project)
          get(url)
          ...accomplish some admin stuff, clicks, and assertions
        end
      end
    
      module CreatorDsl
        def create_paper(name, gen_desc, project)
          ...create a paper, save it, assert things
        end 
      end
    
      ...other modules maybe
    end
    
    

    我知道如何在Capybara切换会话,它 well documented

    但是,有没有一种方法可以用这些“DSL”类型的模块来扩展Capybara会话,类似于上面的模块?当我尝试它,对Capybara语法进行必要的调整时,我得到了 eval error: uninitialized constant Query::Base ;模块中声明的方法似乎在作为Capybara会话的对象类型中不起作用。

    我想知道这是否可能。或者明智!

    0 回复  |  直到 3 年前