我有一個模式關注做驗證:如何在Rails 4中測試模型關注中的驗證?
module UserValidations
extend ActiveSupport::Concern
VALID_EMAIL_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
included do
validates :email,
:presence => true,
:format => VALID_EMAIL_REGEX,
:uniqueness => { case_sensitive: false }
end
end
這種擔憂是用於多種形式。
現在我打算爲這個問題編寫規範,所以我不需要在不同的表單規範中測試同一組驗證。但是,我還沒有發現我已經試過這樣的事情,但沒有一個好辦法做到這一點...
...
require 'spec_helper'
class DummyClass
include ActiveModel::Validations
include ActiveRecord::Validations
include UserValidations
attributes :email, :first_name, :last_name
end
describe "UserValidations" do
subject { DummyClass.new }
it { is_expected.to validate_presence_of :email }
end
錯誤堆棧:
/home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/term-ansicolor-1.3.2/lib/term/ansicolor.rb:239:in `term_ansicolor_attributes': wrong number of arguments (3 for 0) (ArgumentError)
from /home/eyang/grasshopper_dev/gh_user_service/spec/models/concerns/user_validations_spec.rb:8:in `<class:DummyClass>'
from /home/eyang/grasshopper_dev/gh_user_service/spec/models/concerns/user_validations_spec.rb:3:in `<top (required)>'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/bin/rspec:23:in `load'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/bin/rspec:23:in `<main>'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/bin/ruby_executable_hooks:15:in `eval'
from /home/eyang/.rvm/gems/ruby-2.0.0-p594/bin/ruby_executable_hooks:15:in `<main>'
您是如何結束測試驗證? –
我在使用問題的模型中測試了它們。 –
好的,別擔心。不幸的是,我們無法幫助你做你最初想要的東西。 –