2015-10-15 83 views
1

我有一個模式關注做驗證:如何在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>' 
+0

您是如何結束測試驗證? –

+0

我在使用問題的模型中測試了它們。 –

+0

好的,別擔心。不幸的是,我們無法幫助你做你最初想要的東西。 –

回答

0

不知道如何做好以下測試將一個Rails應用程序的環境中工作,但下面是我嘗試在測試只是你儘可能一般地關心這種情況。您應該能夠將代碼複製並粘貼到文件中,然後運行rspec對其進行測試,然後如果它適合您,可以將它集成到您​​的項目中:

require 'active_record' 
require 'active_model' 
require 'active_support' 
require 'shoulda-matchers' 

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 

ActiveRecord::Base.establish_connection(
    adapter: 'sqlite3', database: ':memory:' 
) 

# Create an 'anonymous' table that has an email field 
ActiveRecord::Schema.define do 
    create_table :'' do |t| 
    t.string :email 
    end 
end 

Shoulda::Matchers.configure do |config| 
    config.integrate do |with| 
    with.test_framework :rspec 
    with.library :active_record 
    with.library :active_model 
    end 
end 

RSpec.describe UserValidations, type: :model do 
    let(:user_validate_able) do 
    # Instantiate an anonymous class that inherits from AR::Base 
    Class.new(ActiveRecord::Base) do 
     include UserValidations 

     # ActiveModel needs a model name for error messages, 
     # so give one to this anonymous class 
     def self.model_name 
     ActiveModel::Name.new(self, nil, 'AnonymousModel') 
     end 
    end.new 
    end 

    it 'validates presence, uniqueness, and format of email' do 
    expect(user_validate_able).to validate_presence_of(:email) 
    expect(user_validate_able).to validate_uniqueness_of(:email).case_insensitive 
    expect(user_validate_able).to allow_value('[email protected]').for(:email) 
    end 
end