---
title: "Rails3集成测试插件Rails-Carrot(二)"
author: "myrev"
date: "2010-11-03T11:59:28Z"
tags: []
categories: ["编程语言"]
slug: "rails3-rails-carrot"
draft: false
lastmod: "2010-11-03T11:59:28Z"
id: 801508
url: "https://myrev.iteye.com/blog/801508"
---

本篇简要介绍使用JRuby + RSpec + Rails-Carrot + Celerity 进行rails3项目的集成测试。

 

 

1\. Install jruby

&gt; rvm install jruby

2\. Rails Gemfile

if defined?(JRUBY\_VERSION)

  gem 'activerecord-jdbc-adapter', '1.0.2'

  gem 'jdbc-mysql', :require =&gt; false

  gem 'celerity'

  gem 'rails-carrot', :require =&gt; '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", "&gt;= 2.0.0"

  gem "rspec-rails", "&gt;= 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

&gt;rvm jruby

&gt;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 =&gt; "qaman", :email =&gt; "qaman@test.com"}) 

  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/](http://celerity.rubyforge.org/)

Celerity Github  [http://github.com/jarib/celerity/](http://github.com/jarib/celerity/) 

Celerity Wiki  [http://github.com/jarib/celerity/wiki/\_pages](http://github.com/jarib/celerity/wiki/_pages)

Celerity API  [http://celerity.rubyforge.org/yard/Celerity.html](http://celerity.rubyforge.org/yard/Celerity.html)