3

当我使用 gem 'selenium-webdriver' 运行测试时(带有“ describe "with valid information", :js => true do”)

规范/请求/post_pages_spec.rb

require 'spec_helper'

describe "Post pages" do
  subject { page }  
  category = Category.create(name: "Food")
  let(:post) { FactoryGirl.create(:post) }
     describe "with valid information", :js => true do
      it "should create a post" do
        fill_in "Title", with: post.title
        fill_in "Content", with: post.content
        fill_in "Publish", with: Date.today
        check "Food"  
        expect { click_button "Create Post" }.to change(Post, :count).by(1)
      end
    end
  end

我在规范中遇到错误:

Failures:

  1) Post pages post creation with valid information should create a post
     Failure/Error: expect { click_button "Create Post" }.to change(Post, :count).by(1)
       count should have been changed by 1, but was changed by 0
     # ./spec/requests/post_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

日志/test.log

Started POST "/posts" for 127.0.0.1 at 2012-09-08 01:54:52 +0400
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "post"=>{"title"=>"Lorem ipsum", "content"=>"Lorem ipsum", "category_ids"=>["", "1"], "publish"=>"2012-09-08"}, "commit"=>"Create Post"}
  [1m[35mCategory Load (0.3ms)[0m  SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36m (0.1ms)[0m  [1mbegin transaction[0m
  [1m[35mSQL (5013.9ms)[0m  INSERT INTO "posts" ("content", "created_at", "publish", "title", "updated_at") VALUES (?, ?, ?, ?, ?)  [["content", "Lorem ipsum"], ["created_at", Fri, 07 Sep 2012 21:54:52 UTC +00:00], ["publish", Sat, 08 Sep 2012], ["title", "Lorem ipsum"], ["updated_at", Fri, 07 Sep 2012 21:54:52 UTC +00:00]]
SQLite3::BusyException: database is locked: INSERT INTO "posts" ("content", "created_at", "publish", "title", "updated_at") VALUES (?, ?, ?, ?, ?)
  [1m[36m (0.4ms)[0m  [1mrollback transaction[0m
SQLite3::BusyException: cannot rollback transaction - SQL statements in progress: rollback transaction
Completed 500 Internal Server Error in 5019ms
  [1m[35m (0.1ms)[0m  SELECT COUNT(*) FROM "posts" 
  [1m[36m (0.2ms)[0m  [1mrollback transaction[0m
  [1m[35m (0.3ms)[0m  begin transaction
  [1m[36m (0.1ms)[0m  [1mSAVEPOINT active_record_1[0m
  [1m[35mSQL (0.7ms)[0m  INSERT INTO "posts" ("content", "created_at", "publish", "title", "updated_at") VALUES (?, ?, ?, ?, ?)  [["content", "Lorem ipsum"], ["created_at", Fri, 07 Sep 2012 21:54:57 UTC +00:00], ["publish", Sat, 08 Sep 2012], ["title", "Lorem ipsum"], ["updated_at", Fri, 07 Sep 2012 21:54:57 UTC +00:00]]
  [1m[36m (0.1ms)[0m  [1mRELEASE SAVEPOINT active_record_1[0m


Started GET "/posts" for 127.0.0.1 at 2012-09-08 01:54:57 +0400
Processing by PostsController#index as HTML
  [1m[35mPost Load (0.2ms)[0m  SELECT "posts".* FROM "posts" 
  [1m[36mCategory Load (0.2ms)[0m  [1mSELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."post_id" = 1[0m
  Rendered layouts/_navigation.html.erb (1.1ms)
  Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 25ms (Views: 14.8ms | ActiveRecord: 0.3ms)
  [1m[35m (0.1ms)[0m  SELECT COUNT(*) FROM "posts" 


Started DELETE "/posts/1" for 127.0.0.1 at 2012-09-08 01:54:57 +0400
Processing by PostsController#destroy as HTML
  Parameters: {"id"=>"1"}
  [1m[36mPost Load (0.2ms)[0m  [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m  [["id", "1"]]
  [1m[35m (0.0ms)[0m  SAVEPOINT active_record_1
  [1m[36mSQL (0.2ms)[0m  [1mDELETE FROM "posts" WHERE "posts"."id" = ?[0m  [["id", 1]]
  [1m[35m (0.1ms)[0m  RELEASE SAVEPOINT active_record_1
Redirected to http://www.example.com/posts
Completed 302 Found in 3ms (ActiveRecord: 0.5ms)

但是当我在没有 Selenium ( describe "with valid information" do)的情况下运行测试时,
测试是绿色的。

为什么 Selenium 不创建 Post 并使测试变为红色?
如何解决?

4

1 回答 1

7

解决方案是 Capibara 与 Rspec 的特定工作。
我将以下文件添加到spec/support/database_cleaner.rb

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before :each do
    if Capybara.current_driver == :rack_test
      DatabaseCleaner.strategy = :transaction
    else
      DatabaseCleaner.strategy = :truncation
    end
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end
end

在中添加了宝石Gemfile:

group :development, :test do
  gem 'database_cleaner'
end

$> 捆绑安装

我在下面的行中添加了评论spec/spec_helper.rb

RSpec.configure do |config|
  #config.use_transactional_fixtures = true
end

测试变为绿色。

于 2012-09-08T12:01:28.843 回答