rails_4 アプリケーションのテストを書いています。rspec、selenium、capybara、database_cleaner、site_prism、factory girl を使用しています。
アプリで 2 つのデータベースを使用しています。私のテストでは、1 番目の DB のレコードが 2 番目の DB のレコードと等しいことを確認する必要があります...
しかし、テストを実行すると:
- database_cleaner が「それ」のたびにデータベースをクリーンアップしない
- カテゴリを保存するためにセレンを使用してフォームを投稿すると、カテゴリ/インデックスとカテゴリ/:id/show にこのカテゴリが表示されます。しかし
Category.count returns me 0
!!!
rspec環境を正しくセットアップするのを手伝ってください:
私のspec_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
require 'shoulda/matchers'
require 'selenium-webdriver'
require 'site_prism'
require 'database_cleaner'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.include Capybara::DSL
config.order = "random"
config.color = true
#config.tty = false
#config.formatter = :progress
end
私のサポート/ capybara.rb:
Capybara.register_driver( :selenium ) do |app|
Capybara::Selenium::Driver.new( app, :browser => :firefox )
end
Capybara.configure do |config|
config.default_selector = :css
config.javascript_driver = :selenium
config.default_driver = :selenium
config.app_host = 'http://0.0.0.0:3000'
config.default_wait_time = 3
end
私のサポート/mobile_market_pages.rb:
Dir[Rails.root.join("spec/pages/*_page.rb")].each{ |f| require f }
module MobileMarketPages
class Navigation
include Capybara::DSL
def login_page
LoginPage.new
end
def categories_page
CategoriesPage.new
end
end
end
私のサポート/hooks.rb:
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.start
@page = MobileMarketPages::Navigation.new
@page.login_page.load
@page.login_page.has_email_input?
@page.login_page.has_password_input?
@page.login_page.has_login_button?
User.create!(
email: 'admin12345@gmail.com',
password: 'admin12345',
password_confirmation: 'admin12345'
) if User.count == 0
@page.login_page.email_input.set 'admin12345@gmail.com'
@page.login_page.password_input.set 'admin12345'
@page.login_page.login_button.click
end
config.after(:each) do
DatabaseCleaner.clean
end
end
スペック例:
it 'User create new root category without properties and chieldren' do
@page.categories_page.load
@page.categories_page.has_add_category_button?
@page.categories_page.add_category_button.click
category = FactoryGirl.build(:category)
@page.categories_page.new_category.title.set category.title
@page.categories_page.new_category.description.set category.description
@page.categories_page.new_category.order.set category.order
@page.categories_page.new_category.icon.set category.icon
@page.categories_page.new_category.create_button.click
expect( Category.count ).to eq(1)
expect( Ios::Category.count ).to eq(1)
expect( Android::Category.count ).to eq(1)
[id, title, description, order, icon].each do |param|
postgres_param = Category.first.param
ios_param = Ios::Category.first.param
android_param = Android::Category.first.param
expect( postgres_param ).to eq( ios_param )
expect( postgres_param ).to eq( android_param )
end
end
典型的なエラー:
1) Categories Page User can create new root category User create new root category without properties and chieldren
Failure/Error: expect( Category.count ).to eq(1)
expected: 1
got: 0
(compared using ==)
# ./spec/functional/categories_spec.rb:61:in `block (3 levels) in '
私の工場:
FactoryGirl.define do
factory :value do
value { Faker::Lorem.word }
end
factory :media do
type { 'Image' }
local_path { 'http://www.acsu.buffalo.edu/~rslaine/imageNotFound.jpg' }
order { Faker::Number.number(4) }
end
factory :product do
title { Faker::Lorem.words(2).join(' ') }
description { Faker::Lorem.paragraph(3) }
order { Faker::Number.number(4) }
medias []
values []
end
factory :property do
title { Faker::Lorem.words(2).join('') }
end
factory :category do
title { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.paragraph(3) }
order { Faker::Number.number(4) }
icon { 'http://www.acsu.buffalo.edu/~rslaine/imageNotFound.jpg' }
properties []
products []
end
end
お願い助けて!