当前,当我尝试在valid?
对象上调用headshot_image
或Player
时,测试失败。看完踪迹后,我不确定去哪儿了。谁能指出我可能做错了什么?我可以调用播放器对象上的任何属性,并且它会相应地做出反应,但是对于valid?
和headshot_image
来说,我得到了错误。
Player.rb
class Player < ApplicationRecord
mount_uploader :headshot_image, PlayerHeadshotUploader
end
PlayerHeadshotUploader
class PlayerHeadshotUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}"
end
def default_url
"/uploads/player/no-profile-pic.jpg"
end
version :thumb do
process :resize_to_fit => [50, 50]
end
version :thumb do
process :resize_to_fit => [50, 50]
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
"#{secure_token}.#{file.extension}" if original_filename
end
protected
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end
end
错误和跟踪
(byebug) @player.headshot_image
*** ArgumentError Exception: wrong number of arguments (given 2, expected 0)
nil
(byebug) begin\; @player.headshot_image\; rescue Exception => e\; puts e.backtrace end
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/carrierwave-1.2.3/lib/carrierwave/mount.rb:448:in `initialize'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/carrierwave-1.2.3/lib/carrierwave/mount.rb:448:in `new'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/carrierwave-1.2.3/lib/carrierwave/mount.rb:448:in `_mounter'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/carrierwave-1.2.3/lib/carrierwave/mount.rb:142:in `headshot_image'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/carrierwave-1.2.3/lib/carrierwave/mount.rb:366:in `headshot_image'
(byebug):1:in `block in <class:PlayerTest>'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/helpers/eval.rb:61:in `eval'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/helpers/eval.rb:61:in `safe_eval'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/helpers/eval.rb:55:in `warning_eval'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/helpers/eval.rb:31:in `block in multiple_thread_eval'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/helpers/eval.rb:94:in `allowing_other_threads'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/helpers/eval.rb:31:in `multiple_thread_eval'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/processors/command_processor.rb:163:in `block in run_cmd'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/processors/command_processor.rb:168:in `safely'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/processors/command_processor.rb:159:in `run_cmd'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/processors/command_processor.rb:133:in `repl'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/processors/command_processor.rb:97:in `process_commands'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/processors/command_processor.rb:55:in `at_line'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/byebug-10.0.2/lib/byebug/context.rb:98:in `at_line'
/mnt/c/Users/mcdonaldd/Documents/Rails Projects/bane/test/models/player_test.rb:21:in `block in <class:PlayerTest>'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest/test.rb:98:in `block (3 levels) in run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest/test.rb:195:in `capture_exceptions'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest/test.rb:95:in `block (2 levels) in run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:265:in `time_it'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest/test.rb:94:in `block in run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:360:in `on_signal'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest/test.rb:211:in `with_info_handler'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest/test.rb:93:in `run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:960:in `run_one_method'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:334:in `run_one_method'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:321:in `block (2 levels) in run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:320:in `each'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:320:in `block in run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:360:in `on_signal'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:347:in `with_info_handler'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:319:in `run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/railties-5.1.6/lib/rails/test_unit/line_filtering.rb:9:in `run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:159:in `block in __run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:159:in `map'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:159:in `__run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:136:in `run'
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/minitest-5.11.3/lib/minitest.rb:63:in `block in autorun'
nil
根据请求更新添加player_test.rb
require 'test_helper'
class PlayerTest < ActiveSupport::TestCase
setup do
@player = players(:one)
end
test "first_name is present" do
@player.first_name = nil
assert_not @player.save
end
end