繁体   English   中英

按下提交后,Rails Rspec Capybara Selenium JS创建未显示

[英]Rails Rspec Capybara Selenium JS create not showing after pressing submit

我正在建立一个网上商店,该功能已经存在,即:在一个屏幕中,有一个产品列表和一个订购商品列表。 当按产品订购顺序时,该产品将立即显示在此列表中。

可以猜到,当想要通过硒进行测试时,我看到Firefox正在启动,我想我什至看到按钮被按下,但显然没有任何反应。 我的订单列表中没有项目。

将Rails 5与更新的水豚和硒webdriver一起使用。

gem 'rspec-rails', '~> 3.5', '>= 3.5.1'
gem 'capybara', '~> 2.10', '>= 2.10.1'
gem "factory_girl_rails", "~> 4.7"
gem 'selenium-webdriver', '~> 3.0'
gem 'database_cleaner', '~> 1.5', '>= 1.5.3'  
gem "email_spec", "~> 1.6.0"

creation_order_spec.rb

require 'rails_helper'

RSpec.feature 'User can fill his shopping cart', js: true do
  let!(:category) { FactoryGirl.create(:category, name: 'Honey') }
  let!(:cheap_product) { FactoryGirl.create(:product, name: 'Honey lolly', 
                                                      price: '0,80',
                                                      category: category) }
  let!(:expensive_product) { FactoryGirl.create(:product, name: 'Honeyjar 400ml', 
                                                      price: '3,95',
                                                      category: category) }

  before do
    visit categories_path
  end

  scenario 'with different products' do
    page.find('.product', :text => 'Honey lolly').click_button('ADD TO CART')
    page.find('.product', :text => 'Honeyjar 400ml').click_button('ADD TO CART')

    within('#order') do
      expect(page).to have_content 'Honey lolly'
      expect(page).to have_content 'Honeyjar 400ml'
      expect(page).to have_content 0.80 + 3.95
    end
  end
end

Database_cleaning.rb

RSpec.configure do |config|  
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.use_transactional_fixtures = false

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end    

  config.before(:each) do
    DatabaseCleaner.start
  end

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

然后我运行它并得到:

Failure/Error: expect(page).to have_content 'Honey lolly'
       expected to find text "Honey lolly" in "Total € 0,00"

这意味着我的订单没有任何反应,或者总金额超过了€0,00。

编辑

这是仅在某种程度上在测试中不起作用的JS。

$('#order').html("<%= j render 'orders/order' %>")

编辑2

测试日志

 Started POST "/bookings" for 127.0.0.1 at 2016-11-14 10:11:00 +0100 Processing by BookingsController#create as JS Parameters: {"utf8"=>"✓", "booking"=>{"product_id"=>"1", "product_quantity"=>"5"}} [1m[36mProduct Load (0.5ms)[0m [1m[34mSELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]] [1m[35m (0.4ms)[0m [1m[35mBEGIN[0m [1m[36mBooking Exists (0.7ms)[0m [1m[34mSELECT 1 AS one FROM "bookings" WHERE "bookings"."order_id" IS NULL AND "bookings"."product_id" = $1 LIMIT $2[0m [["product_id", 1], ["LIMIT", 1]] [1m[35m (0.1ms)[0m [1m[31mROLLBACK[0m Rendering bookings/create.js.erb Rendered orders/_order.html.erb (0.9ms) Rendered bookings/create.js.erb (2.8ms) Completed 200 OK in 14ms (Views: 4.8ms | ActiveRecord: 2.2ms) 

似乎没有为该订单触发SQL语句。 通常,应该填写总数。

bookings_controller.rb

def create
  @booking = @order.bookings.find_by(product_id: params[:booking][:product_id])
  if @booking
    @booking.product_quantity = params[:booking][:product_quantity]
    @booking.save
  else
    @booking = @order.bookings.new(booking_params)
    @product = @booking.product
    @booking.product_name = @product.name
    @booking.product_price = @product.price
  end
  @order.save
  respond_to do |format|
      format.html { redirect_to categories_path }
      format.js { render layout: false }
  end
end

order.save触发一个名为.sum_all_bookings的额外方法。 这没有显示在SQL中。 此处的错误位于视图(#order)中顺序不变的错误处。

最可能的原因是您的JS资产之一发生错误,导致您预期的行为无法正常执行。 当资产未连接时,事情可以在开发模式下正常工作(因此,一个文件中的错误不会阻止其他文件的处理),但是当一个文件中的错误可以阻止资产时,则在测试和生产环境中会失败解析/处理后并置的JS。

另外,您会发现2个(id为=产品('#product')的元素(我假设它们会有所不同,因为您希望添加2个产品)。 这是无效的html,因为id必须是唯一的,因此可能导致奇怪的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM