使用RSpec测试多个自定义验证器

时间:2013-03-28 23:47:09

标签: ruby-on-rails-3 validation rspec

我正在尝试为两个自定义验证器运行规范:

spec/validators/email_validator_spec.rb
spec/validators/phone_validator_spec.rb

当我运行bundle exec rspec spec/validators/ phone_validator_spec.rb规范失败时:

1) PhoneValidator with a valid phone number should be valid
     Failure/Error: subject.should be_valid
       expected valid? to return true, got false
     # ./spec/validators/phone_validator_spec.rb:20:in `block (4 levels) in <top (required)>'
     # ./spec/validators/phone_validator_spec.rb:18:in `each'
     # ./spec/validators/phone_validator_spec.rb:18:in `block (3 levels) in <top (required)>'

但是,当我使用命令bundle exec rspec spec/validators/phone_validator_spec.rb单独运行该规范时,它会通过。

当我使用email_validator_spec.rb命令删除phone_validator_spec.rb然后bundle exec rspec spec/validators/次传递时。

我希望在运行bundle exec rspec spec/validators/时,两个规格都会通过。谁能向我解释发生了什么?

更新 使用zetetic的提示打印错误哈希:

1) PhoneValidator with a valid phone number should be valid
     Failure/Error: subject.errors.should == {}
       expected: {}
            got: #<ActiveModel::Errors:0x37b2460 @base=#<Validatable:0x37b2700 @validation_context=nil, @errors=#<ActiveModel::Errors:0x37b2460 ...>, @phone_number="1112223333">, @messages={:email=>["is invalid"]}> (using ==)
       Diff:
       @@ -1 +1,8 @@
       +#<ActiveModel::Errors:0x37b2460
       + @base=
       +  #<Validatable:0x37b2700
       +   @errors=#<ActiveModel::Errors:0x37b2460 ...>,
       +   @phone_number="1112223333",
       +   @validation_context=nil>,
       + @messages={:email=>["is invalid"]}>
     # ./spec/validators/phone_validator_spec.rb:21:in `block (4 levels) in <top (required)>'
     # ./spec/validators/phone_validator_spec.rb:18:in `each'
     # ./spec/validators/phone_validator_spec.rb:18:in `block (3 levels) in <top (required)>'

在运行两个规范时,似乎会合并Validatable类定义。这种行为有望吗?如果我使用不同的类名,则两个规范都会通过。

规格/验证/ phone_validator_spec.rb

require 'active_model'
require 'rspec/rails/extensions'
require File.expand_path('app/validators/phone_validator')

class Validatable
  include ActiveModel::Validations
  attr_accessor :phone_number
  validates :phone_number, phone: true
end

describe PhoneValidator do

  subject { Validatable.new }

  describe "with a valid phone number" do
    it "should be valid" do
      phone_numbers = ["1112223333", "123222ABCD"]
      phone_numbers.each do |phone_number|
        subject.phone_number = phone_number
        subject.should be_valid
      end
    end 
  end
end

应用/验证/ phone_validator.rb

class PhoneValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    return if value.blank?
    unless value =~ /^[A-Za-z0-9]{10}$/
      object.errors[attribute] << (options[:message] || "is not formatted properly")
    end
  end
end

规格/验证/ email_validator_spec.rb

require 'active_model'
require 'rspec/rails/extensions'
require File.expand_path('app/validators/email_validator')

class Validatable
  include ActiveModel::Validations
  attr_accessor :email
  validates :email, email: true
end

describe EmailValidator do

  subject { Validatable.new }

  describe "with a valid email address" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        subject.email = valid_address
        subject.should be_valid
      end
    end
  end

  describe "with an invalid phone number" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo]
      addresses.each do |invalid_address|
        subject.email = invalid_address
        subject.should be_invalid
      end
    end
  end
end

应用/验证/ email_validator.rb

require 'mail'

class EmailValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    begin
      m = Mail::Address.new(value)
      # We must check that value contains a domain and that value is an email address
      r = m.domain && m.address == value
      t = m.__send__(:tree)
      # We need to dig into treetop
      # A valid domain must have dot_atom_text elements size > 1
      # user@localhost is excluded
      # treetop must respond to domain
      # We exclude valid email values like <user@localhost.com>
      # Hence we use m.__send__(tree).domain
      r &&= (t.domain.dot_atom_text.elements.size > 1)
    rescue => e   
      r = false
    end
    object.errors[attribute] << (options[:message] || "is invalid") unless r
  end
end

使用rails 3.2.11,rspec-rails 2.11.0

1 个答案:

答案 0 :(得分:0)

我自己也遇到了这个问题,是的,您可以重命名该类,但我使用的解决方案是在您的规范中创建和拆除Validatable类。

以下是代码段:

describe "HttpUriValidator",
  "Custom validator to ensure URL is a valid URI." do

  # Create the dummy class once when the test is run.
  before(:all) do
    class Validatable
      include ActiveModel::Validations
      attr_accessor  :url
      validates :url, http_uri: true
    end
  end
  # Must tearing down the class or it will taint other tests using its
  # name.
  after(:all) { Object.send(:remove_const, :Validatable) }

  subject { Validatable.new }

EDIT ::

当您声明一个模块包装您的测试类时(为了避免在测试中使用其他类的名称),只需要抬头即可。

module Foo::Bar
  describe Something do
    after(:all) { Foo::Bar.send(:remove_const, :Testable) }
  end
end

您必须从该命名空间中删除常量,而不是Object。