Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@wcareer

Capybara、phantomjsを使ったテスト

More than 5 years have passed since last update.

JavascriptによるDOM操作に依存する総合テスト

Jsのテスト環境構築をメモ

通常の Rspec/Capybara のテストとは分離します。
理由:Js関連と分けることで、テストの実行にかかる時間を短縮
場所: jspec/features ディレクトリを作成

準備 (Mac)

$ cd ~
$ mkdir -p src
$ cd src
$ git clone git://github.com/ariya/phantomjs.git
$ cd phantomjs
$ git checkout 1.9
$ ./build.sh
$ sudo cp bin/phantomjs /usr/local/bin/
$ brew install phantomjs192

※ buildには20分程かかります。

Gemfile
Capybara から PhantomJS を使うには poltergeist を使います。

group :test do
  gem 'rspec-rails'
  gem 'capybara'
  gem 'poltergeist', git: 'git@github.com:teampoltergeist/poltergeist.git'
  gem 'launchy'
end

spec_helper.rb
1, inspector: true Poltergeistが提供しているデバッグ機能
https://github.com/teampoltergeist/poltergeist#remote-debugging-experimental
2, poltergeistを使う指定

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/poltergeist'

Capybara.register_driver :poltergeist do |app|
  options = { inspector: true }
  Capybara::Poltergeist::Driver.new(app, options)
end

Capybara.javascript_driver = :poltergeist
Capybara.always_include_port = true

Dir[Rails.root.join("jspec/support/**/*.rb")].each { |f| require f }

テスト実行には下記のコマンドを使用します。

$ bin/rspec --default-path=jspec jspec/

これで、Jsを含んだ機能のテストができます。

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
wcareer
技術的メモに使っています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?