我正在使用Rspec和Capybara进行功能测试。
当我将附件添加到浏览器上的现有文章时,我没有问题... 但是当我运行测试时,那失败了,我不知道发生了什么... 有什么问题吗?
那是完整的错误:
Failures:
1) managing article The admin is logged in add images for an article
Failure/Error: flash[:error] = "Failed uploading attachments" unless @article.save
NoMethodError:
undefined method `reject' for #<String:0x007ffc976cb9e8>
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/carrierwave-1.2.3/lib/carrierwave/mounter.rb:130:in `remove_previous'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/carrierwave-1.2.3/lib/carrierwave/mount.rb:349:in `remove_previously_stored_attachments'
# ./app/controllers/attachments_controller.rb:6:in `create'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/tempfile_reaper.rb:15:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/etag.rb:25:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/conditional_get.rb:38:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/head.rb:12:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-l2.0.5/lib/rack/session/abstract/id.rb:232:in `context'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:226:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/rack/logger.rb:38:in `call_app'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `block in call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/engine.rb:524:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/urlmap.rb:68:in `block in call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `each'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `call'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-test-1.1.0/lib/rack/test.rb:129:in `custom_request'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/rack-test-1.1.0/lib/rack/test.rb:66:in `post'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/rack_test/browser.rb:75:in `process'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/rack_test/browser.rb:42:in `process_and_follow_redirects'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/rack_test/browser.rb:33:in `submit'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/rack_test/form.rb:46:in `submit'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/rack_test/node.rb:69:in `click'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/node/element.rb:154:in `block in click'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/node/base.rb:82:in `synchronize'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/node/element.rb:154:in `click'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/node/actions.rb:58:in `click_button'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/session.rb:728:in `block (2 levels) in <class:Session>'
# /Users/bill/.rvm/gems/ruby-2.4.1/gems/capybara-3.7.1/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/features/managing_article_spec.rb:54:in `block (3 levels) in <top (required)>'
这是我失败的测试的一部分:
scenario "add images for a guitar" do
guitar = articles(:gibson)
visit article_path(guitar)
click_on "Add pictures"
within "#label_uploader_2" do
within "#span_uploader_2" do
attach_file("article[attachments][]" , Rails.root.join("spec/assets/images/img_2.jpg"))
end
end
click_on "Update Finished guitar"
expect(page).to have_content("Picture was successfully added")
end
这是我的附件控制器
class AttachmentsController < ApplicationController
before_action :set_article
def create
add_more_attachments(attachments_params[:attachments])
if @article.save
redirect_to @article, notice: "Picture was successfully added"
else
flash[:error] = "Failed uploading attachments"
end
end
def destroy
remove_attachments_at_index(params[:id].to_i)
if @article.save
redirect_to @article, notice: "Picture was successfully destroyed"
else
flash[:error] = "Failed deleting attachments"
end
end
private
def set_article
@article = FinishedGuitar.find(params[:article_id])
end
def add_more_attachments(new_attachments)
attachments = @article.attachments
attachments += new_attachments
@article.attachments = attachments
end
def remove_attachments_at_index(index)
remain_attachments = @article.attachments # copy the array
deleted_attachments = remain_attachments.delete_at(index) # delete the target attachments
deleted_attachments.try(:remove!) # delete attachments from S3
@article.attachments = remain_attachments # re-assign back
end
def attachments_params
params.require(:article).permit({attachments: []}) # allow nested params as array
end
end
如果我移除固定装置中的attachments
行,则测试为绿色...这个想法是测试图片的添加。
也许我还没有设置好测试条件
我的article.yml固定装置
gibson:
title: Gibson Les Paul
description: Une jolie description pour une jolie guitare.
attachments: <%= Rails.root.join("spec/assets/images/img_2.jpg") %>
运行测试后,这是我的数据库日志:
Completed 200 OK in 24ms (Views: 20.9ms | ActiveRecord: 0.6ms)
Started POST "/articles/1072060877/attachments" for 127.0.0.1 at 2018-09-11 23:03:45 +0200
Processing by AttachmentsController#create as HTML
Parameters: {"utf8"=>"✓", "article"=>{"attachments"=>[#<ActionDispatch::Http::UploadedFile:0x00007fc76d6838a0 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20180911-67380-ir0qv2.jpg>, @original_filename="img_2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[attachments][]\"; filename=\"img_2.jpg\"\r\nContent-Type: image/jpeg\r\nContent-Length: 232558\r\n">]}, "commit"=>"Update article", "article_id"=>"1072060877"}
Article Load (0.4ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2 [["id", 1072060877], ["LIMIT", 1]]
(0.3ms) SAVEPOINT active_record_1
Article Update (0.5ms) UPDATE "articles" SET "attachments" = $1, "updated_at" = $2 WHERE "articles"."id" = $3 [["attachments", "[null,\"img_2.jpg\"]"], ["updated_at", "2018-09-11 21:03:45.472070"], ["id", 1072060877]]
(0.2ms) RELEASE SAVEPOINT active_record_1
Completed 500 Internal Server Error in 172ms (ActiveRecord: 1.3ms)
(0.3ms) ROLLBACK
答案 0 :(得分:1)
我相信这里的问题是,您支持文章上的多个附件,但是在夹具中,您是将单个字符串分配给附件,而不是将包含单个字符串的数组分配给附件。尝试将灯具更改为
gibson:
title: Gibson Les Paul
description: Une jolie description pour une jolie guitare.
attachments: [<%= Rails.root.join("spec/assets/images/img_2.jpg") %>]