本应失败的 RSPEC 测试已通过

发布于 2024-12-04 07:27:18 字数 1439 浏览 0 评论 0原文

我使用 rspec、rails、guard 和 sorcery 进行身份验证和测试。

我有一个测试正在测试电子邮件的长度。我想拒绝太长的电子邮件。这是我为 spec/models/user_spec.rb 编写的测试

    require 'spec_helper'

    describe User do
     before(:each) do
       @attr = { :email => "[email protected]", :password => "password", :password_confirmation => "password" }
     end


     it "should reject emails that are too long" do
       long_email = "a" * 101 + "gmail.com"
       long_email = User.new (@attr.merge(:email => long_email))
       long_email.should_not be_valid
     end

这是我已有的模型验证:

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  attr_accessible :email, :password, :password_confirmation

  validates_presence_of :password, :on => :create
  validates :password, :confirmation => true,
                       :length       => { :within => 6..100 }

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, :presence        => true,
                    :format          => { :with => email_regex },
                    :uniqueness      => {:case_sensitive => false},
                    :length          => { :within => 5..100 }
end

我是这方面的菜鸟,所以任何帮助将不胜感激。测试现在是绿色的,如果我将行更改为 long_email.should be_valid,它会变成红色。提前致谢!

I'm using rspec, rails, guard and sorcery for my authentication and testing.

I have a test that is testing the length of an email. I want to reject emails that are too long. Here is the test I wrote for spec/models/user_spec.rb

    require 'spec_helper'

    describe User do
     before(:each) do
       @attr = { :email => "[email protected]", :password => "password", :password_confirmation => "password" }
     end


     it "should reject emails that are too long" do
       long_email = "a" * 101 + "gmail.com"
       long_email = User.new (@attr.merge(:email => long_email))
       long_email.should_not be_valid
     end

Here is the model validations I have in place:

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  attr_accessible :email, :password, :password_confirmation

  validates_presence_of :password, :on => :create
  validates :password, :confirmation => true,
                       :length       => { :within => 6..100 }

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, :presence        => true,
                    :format          => { :with => email_regex },
                    :uniqueness      => {:case_sensitive => false},
                    :length          => { :within => 5..100 }
end

I'm a noob with this stuff, so any help would be greatly appreciated. The test is green right now, it goes red if I change the line to long_email.should be_valid. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-12-11 07:27:18

正如@apneadiving 指出的,测试的格式很重要。它不仅使其更易于阅读,而且还显示您尚未测试的地方:

require 'spec_helper'

describe User do
  let(:attr){ 
    {:email => "[email protected]", :password => "password", :password_confirmation => "password" }
  }

  context "When given an email address" do
    context "That is too long" do
      let(:long_email){ "a" * 101 + "gmail.com" }
      subject{ User.new attr.merge(:email => long_email) }

      specify{ subject.should_not be_valid }
    end
    context "That is too short" do
      pending
    end
    context "That is between 5 and 100 characters long"
      pending
    end
  end

end

如您所见,重写它做了几件事:

  • 使其更易于阅读,
  • 更容易添加更多测试,而不会影响早期的测试那些(通过使用 let 而不是 before
  • 表明您只指定了失败的情况,并且没有测试积极的情况(或其他情况)

如果您写积极的情况也过去了,那么你就真的知道出了问题!

As @apneadiving points out, the format of test is important. It not only makes it easier to read, but also shows up places you haven't tested:

require 'spec_helper'

describe User do
  let(:attr){ 
    {:email => "[email protected]", :password => "password", :password_confirmation => "password" }
  }

  context "When given an email address" do
    context "That is too long" do
      let(:long_email){ "a" * 101 + "gmail.com" }
      subject{ User.new attr.merge(:email => long_email) }

      specify{ subject.should_not be_valid }
    end
    context "That is too short" do
      pending
    end
    context "That is between 5 and 100 characters long"
      pending
    end
  end

end

As you can see, rewriting it has done several things:

  • made it easier to read
  • made it easier to add more tests without having earlier tests impact later ones (by using let instead of before)
  • shown you that you've only specified a failing case, and not tested for the positive case (or other cases)

If you write the positive case and that passes too then you really know something is wrong!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文