0

rails_4 응용 프로그램에 대한 테스트를 작성하고 있습니다. rspec, selenium, capybara, database_cleaner, site_prism 및 factory 소녀를 사용합니다.Rspec가 DB에 레코드를 저장하지 않습니다

나는 내 응용 프로그램에서이 데이터베이스를 사용하고, 나를 시험에 나는 ... 1 DB의 레코드가 2 DB에서 동일한 기록이 있음을

를 확인해야하지만 내 테스트를 실행하면

  • database_cleaner는 모든 "it"후에 데이터베이스를 정리하지 않습니다.
  • 셀렌을 사용하여 범주를 저장하면이 범주가 범주/인덱스 및 범주/: 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 

,363,210

내 지원/hooks.rb :



    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 

나를 도와 사양의



    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:     '[email protected]', 
      password:    'admin12345', 
      password_confirmation: 'admin12345' 
     ) if User.count == 0 

     @page.login_page.email_input.set '[email protected]' 
     @page.login_page.password_input.set 'admin12345' 
     @page.login_page.login_button.click 
     end 

     config.after(:each) do 
     DatabaseCleaner.clean 
     end 

    end 

예부디!

답변

0

문제는 내 모델의 인스턴스 속성을 비교하는 데있었습니다. spec 파일을 다음으로 변경했습니다.



    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   'http://www.acsu.buffalo.edu/~rslaine/imageNotFound.jpg' 
     @page.categories_page.new_category.create_button.click 

     expect(Category.first.id).to eq(Ios::Category.first.id ) 
     expect(Category.first.id).to eq(Android::Category.first.id) 

     %w(title description order icon).each do |param| 
     postgres_param = Category.first.send(param) 
     ios_param  = Ios::Category.first.send(param) 
     android_param = Android::Category.first.send(param) 

     expect(postgres_param).to eq(ios_param ) 
     expect(postgres_param).to eq(android_param) 
     end  
    end 

이해할 수 없으므로 트랜잭션이 실패하기 때문에 데이터가 DB에 기록되지 않았습니다.

관련 문제