Ruby on rails 3 使用RSpec测试多个自定义验证器

Ruby on rails 3 使用RSpec测试多个自定义验证器,ruby-on-rails-3,validation,rspec,Ruby On Rails 3,Validation,Rspec,我正在尝试为两个自定义验证器运行规范: spec/validators/email_validator_spec.rb spec/validators/phone_validator_spec.rb 当我运行bundle exec rspec spec/validators/phone\u validator\u spec.rbspec失败: 1) PhoneValidator with a valid phone number should be valid Failure/Err

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

spec/validators/email_validator_spec.rb
spec/validators/phone_validator_spec.rb
当我运行
bundle exec rspec spec/validators/
phone\u validator\u spec.rbspec失败:

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)>'
当运行两个规范时,将合并
可验证的
类定义。这是预期的行为吗?如果我使用不同的类名,两个规范都会通过

spec/validators/phone\u validator\u 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
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
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
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
app/validators/phone_validator.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
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
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
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
app/validators/email\u validator.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
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
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
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
需要“邮件”
类EmailValidator1)
救援=>e
r=假
结束

object.errors[attribute]您的模型实例无效,但您不知道原因。试着改变

subject.should be_valid

现在,失败消息将打印出错误哈希

另一个提示:

编辑

运行两个规范时,可验证类定义似乎合并在一起。这是预期的行为吗

是的,这对于Ruby类来说是正常的。当两个spec文件都是必需的时,将执行每个
Validatable
类主体,因此最终得到一个包含这两个验证的类

您需要通过使受试者通过未测试的验证来隔离验证,例如:

subject { Validatable.new(:email => "some value") }
或测试被测验证中的特定错误消息:

subject.valid?
subject.errors(:email).should include("is invalid")

注意:认真地说,不要营救例外。没有什么好结果。

我自己也遇到了这个问题,是的,你可以重命名这个类,但我使用的解决方案是在你的规范中创建并拆除可验证的类

下面是一段代码片段:

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 }
编辑::

当您声明一个模块包装您的测试类(以避免在测试中给其他类命名)ie时,请注意


您必须从该名称空间中删除常量,而不是对象。

谢谢您的提示。似乎是两个
可验证的
类定义导致了问题。我已经更新了我的问题。谢谢更新。我不希望规范相互依赖,所以我只给每个可验证类一个唯一的名称。我接受了你的建议,也删除了营救例外。