[英]Rails Rspec Capybara Selenium JS create not showing after pressing submit
I am building a web shop, the functionality is already there, namely: in ONE screen there is a list of products and a list of ordered items. 我正在建立一个网上商店,该功能已经存在,即:在一个屏幕中,有一个产品列表和一个订购商品列表。 When in a product pressing order, this product then shows up immediately in this list. 当按产品订购顺序时,该产品将立即显示在此列表中。
As you can guess, when wanting to do this with a test, using selenium I see Firefox starting up, I think I even see the button being pressed, but then obviously nothing happens. 可以猜到,当想要通过硒进行测试时,我看到Firefox正在启动,我想我什至看到按钮被按下,但显然没有任何反应。 No item in my order list. 我的订单列表中没有项目。
Using Rails 5 with an updated capybara and selenium webdriver. 将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"
creating_order_spec.rb 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 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
Then I run it and get: 然后我运行它并得到:
Failure/Error: expect(page).to have_content 'Honey lolly'
expected to find text "Honey lolly" in "Total € 0,00"
Which means that nothing has happened to my order, or the total would have been more than € 0,00. 这意味着我的订单没有任何反应,或者总金额超过了€0,00。
EDIT 编辑
This is the JS that only somehow doesn't work in tests. 这是仅在某种程度上在测试中不起作用的JS。
$('#order').html("<%= j render 'orders/order' %>")
EDIT 2 编辑2
test.log 测试日志
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)
Seems that there is no SQL statement triggered for the order. 似乎没有为该订单触发SQL语句。 While normally it should be to fill in the total. 通常,应该填写总数。
bookings_controller.rb 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
triggers an extra method called .sum_all_bookings
. order.save
触发一个名为.sum_all_bookings
的额外方法。 This is not showing up in the SQL. 这没有显示在SQL中。 Herein lies somewhere the error of the order staying unchanged in the view (#order). 此处的错误位于视图(#order)中顺序不变的错误处。
Most likely cause is an error in one of your JS assets that's preventing the behavior you're expecting from being attached. 最可能的原因是您的JS资产之一发生错误,导致您预期的行为无法正常执行。 Things can work fine in dev mode when the assets aren't concatenated (so an error in one file doesn't prevent the other files from being processed), but then fail in the test and production environments when an error in one file can prevent the JS concatenated after it from being parsed/processed. 当资产未连接时,事情可以在开发模式下正常工作(因此,一个文件中的错误不会阻止其他文件的处理),但是当一个文件中的错误可以阻止资产时,则在测试和生产环境中会失败解析/处理后并置的JS。
Additionally you find 2 (I assume they are different since you're expecting 2 products to be added) elements with id = Product ('#product'). 另外,您会发现2个(id为=产品('#product')的元素(我假设它们会有所不同,因为您希望添加2个产品)。 That is invalid html since ids are required to be unique, so could lead to weird issues. 这是无效的html,因为id必须是唯一的,因此可能导致奇怪的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.