Ruby 的 Jasmine 已停用,我們建議改用 jasminejasmine-browser-runner NPM 套件。

使用 ruby 搭配 jasmine

即使不搭配 Rails,也能使用 jasmine gem。(注意:jasmine gem 僅支援 Asset Pipeline,而不支援 Webpacker。 jasmine-browser-runner 適用於兩種風格的 Rails JavaScript 管理。)

將 jasmine gem 加到 gemfile 中,並執行 bundle install

gem 'jasmine'

設定

若要安裝預設的 jasmine.yml 和範例 jasmine_helper.rb,如果專案使用 Rails,可以使用 jasmine 的產生器來設定所有內容。

rails g jasmine:install

如果專案不使用 Rails,命令列工具 jasmine 中也具有所有這些指令。命令列工具也會修改 Rakefile 以載入 jasmine 工作。

jasmine init

Jasmine 另外提供一些範例規格(含實作),你可以安裝它們。

rails g jasmine:examples
jasmine examples

使用

安裝 jasmine.yml 之後,可以在 rake 中使用兩項指令。

如果你想啟動持續執行的伺服器,以便指向瀏覽器

rake jasmine

在 CI 伺服器上使用

rake jasmine:ci

對於個別的 jasmine:ci 執行,你也可以從 jasmine.yml 中覆寫隨機化設定,以進行偵錯用。

rake jasmine:ci[true]

也可以指定種子

rake jasmine:ci[true,4321]

至此應該可以 撰寫第一個套件

設定

主要設定在 jasmine.yml 中進行,預設路徑為 spec/javascripts/supportjasmine.yml 設定了執行測試時應在網頁中載入哪些檔案,以及載入 ruby 檔案的位置,用於更複雜的設定,示範如下

jasmine_helper.rb 檔案由 jasmine.yml 中的 spec_helper 關鍵字指定,包含一個設定區塊。

Jasmine.configure do |config|
  # You can add rack handlers for specific urls
  config.add_rack_path '/something' do
    [200]
  end
  # And mount other rack applications
  config.add_rack_app MyRackApp
  # You can configure the port that the `rake jasmine` command starts a server on
  config.server_port = 12345
  # You can configure the port that the `rake jasmine:ci` command starts it's server on
  config.ci_port = 54321
  # You can add [custom formatters](#section-Custom_Formatters)
  config.formatters << My::Custom::Formatter
  # You can use a [custom runner](#section-Custom_Runners)
  # The `runner` option on config should be a lambda or Proc that receives a formatter
  # and server url and returns a constructed runner object. The lambda allows you to
  # configure other options that need to be configured at initialization time.
  config.runner = lambda do |formatter, server_url|
    My::Custom::Runner.new(formatter, server_url, 100)
  end
end

設定預設 phantomjs 跑者

phantomjs 跑者支援一些其他選項。

如果想看到規格中 console.log 訊息的輸出,請將 show_console_log 設為 true。

show_console_log: true

如果需要設定 phantomjs 網頁物件,你可以指定設定指令碼。

phantom_config_script: 'relative/path/from/project/root.js'

這個檔案將會被 phantom 跑者 require,而 configure 函式會傳遞給建構的 page 物件。

exports.configure = function(page) {
  page.viewportSize = {
    width: 340,
    height: 220
  };
};

自訂格式化工具

預設 jasmine:ci rake 任務輸出 .F、和 * 當作規格運行和摘要的結果。如果你想要變更輸出,或提供其他輸出,你需要一個自訂格式器。關於如何新增自訂格式器的範例,請見 Configuration

自訂格式器必須包含兩個方法,format and done

class My::Custom::Formatter
  # `format` is called by the runner every time it gets a batch of results from the page.
  # The parameter will be an array of `Jasmine::Result` objects
  def format(results)
    results.each do |result|
      puts result.status
    end
  end

  # `done` will be called by the runner after all results have come in.
  def done
    puts 'Done running tests'
  end
end

jasmine 團隊也維護一個能提供 junit 格式 XML 的自訂格式器,給知道如何解析的 CI 伺服器使用。 Jasmine JUnit XML 格式器

自訂執行器

預設 jasmine:ci rake 任務使用 phantomjs 載入 jasmine 規格執行器頁面並執行測試。假如你想要使用不同瀏覽器執行測試,或變更 phantom 的使用方式,你將會需要一個自訂執行器。關於如何新增自訂執行器的範例,請見 configuration section

建構完成後,執行器只需要實作一個 run 方法

class My::Custom::Runner
  def initialize(formatter, jasmine_server_url, result_batch_size)
    # The formatter passed in is responsible for making sure all configured
    # formatters receive the same messages.
    @formatter = formatter
    # The `jasmine_server_url` is the full http://<host>:<port> url where
    # the jasmine server was started
    @jasmine_server_url = jasmine_server_url
    @result_batch_size = result_batch_size
  end

  # `run` is responsible coordinating the test run.
  def run
    # Here we're using Phantom to load the page and run the specs
    command = "#{Phantomjs.path} 'phantom_run.js' #{@jasmine_server_url} #{@result_batch_size}"
    IO.popen(command) do |output|
      # The `phantom_jasmine_run.js` script writes out batches of results as JSON
      output.each do |line|
        raw_results = JSON.parse(line, :max_nesting => false)
        # Formatters expect to get `Jasmine::Result` objects.
        # It is the runner's job to convert the result objects from the page,
        # and pass them to the `format` method of their formatter.
        results = raw_results.map { |r| Result.new(r) }
        @formatter.format(results)
      end
    end
    # When the tests have finished, call `done` on the formatter to run any
    # necessary completion logic.
    @formatter.done
  end

  # If the runner needs some javascript to be loaded into the page as part of the load,
  # it returns the full path in `boot_js`
  def boot_js
    File.expand_path('runner_boot.js', __FILE__)
  end
end

jasmine 團隊也維護一個自訂執行器,他使用 selenium (還有 SauceLabs) 來執行你的規格與其他瀏覽器。 Jasmine Selenium Runner