本篇简要介绍使用JRuby + RSpec + Rails-Carrot + Celerity 进行rails3项目的集成测试。
1. Install jruby
> rvm install jruby
if defined?(JRUBY_VERSION)
gem 'activerecord-jdbc-adapter', '1.0.2'
gem 'jdbc-mysql', :require => false
gem 'celerity'
gem 'rails-carrot', :require => 'carrot'
else
gem 'mysql2'
group :celerity do # celerity 是项目测试环境,根据喜好自己修改
gem 'mysql'
end
end
|
Other test gems
group :development, :test do
gem "database_cleaner"
gem 'webrat'
gem "rspec", ">= 2.0.0"
gem "rspec-rails", ">= 2.0.0"
end
|
3. Add Rails Environment
database.yml
celerity:
adapter: mysql
encoding: utf8
reconnect: false
database: AC_rails3_celerity
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
#{Rails.root}/config/environments/celerity.rb
ACRails3::Application.configure do
config.cache_classes = true
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_dispatch.show_exceptions = false
config.action_mailer.delivery_method = :test
config.active_support.deprecation = :stderr
end
4. Install Gems
>rvm jruby
>bundle install
5. RSpec helper
Create a RSpec helper and you can use it to test with Celerity.
#{Rails.root}/spec/Celerity_helper
require 'database_cleaner'
require 'celerity'
require 'carrot'
ENV["RAILS_ENV"] = 'celerity'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Carrot.configure do |config|
config.run_server = true
config.external_ruby = true
config.rails_command = "~/.rvm/gems/ruby-1.9.2-p0/bin/rails s -e celerity -p 3001"
config.project_path = "#{Rails.root}"
config.server_port = 3001
# config.server_debug = true
end
Carrot.register_driver(Celerity::Browser.new)
RSpec.configure do |config|
DatabaseCleaner.strategy = :truncation
config.before :each do
@browser = Carrot.driver
DatabaseCleaner.clean
end
config.after :all do
DatabaseCleaner.clean
end
config.use_transactional_fixtures = false
def url(path)
Carrot.url(path)
end
def login(browser, login, password)
browser.text_field(:name, 'login').value = login
browser.text_field(:name, 'password').value = password
browser.button(:name, 'commit').click
end
end
|
6. Example
#{Rails.root}/spec/requests/hello_spec.rb
require 'celerity_helper'
describe "QA" do
before do
#以下为业务数据,直接插入数据到数据库
@company = create_company
@user = create_company_admin(@company, {:login => "qaman", :email => "[email protected]"})
end
it "should redirect to main page" do
@browser.goto(url('/'))
login(@browser, @user.login, "123456")
@browser.text.should =~ /Projects of Shepherd/
end
end
|
7. Resource
Celerity Home http://celerity.rubyforge.org/
Celerity Github http://github.com/jarib/celerity/
Celerity Wiki http://github.com/jarib/celerity/wiki/_pages
Celerity API http://celerity.rubyforge.org/yard/Celerity.html