Capybara-selenium故障和重定向example.com/当没有一切都是绿色

时间:2012-09-07 22:13:55

标签: ruby-on-rails selenium rspec capybara capybara-webkit

当我使用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并将测试设为红色? 如何解决?

1 个答案:

答案 0 :(得分:6)

解决方案是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

$&GT;捆绑安装

我在spec/spec_helper.rb

中的以下行添加评论
RSpec.configure do |config|
  #config.use_transactional_fixtures = true
end

测试变为绿色。