应该测试格式

发布于 2024-09-04 13:05:26 字数 2664 浏览 2 评论 0原文

我正在尝试用这个来测试模型的格式化属性:

# myapp/test/unit/comment_test.rb
require 'test_helper'

class CommentTest < ActiveSupport::TestCase
  should_belong_to :article

  should_validate_presence_of :name
  should_validate_presence_of :email
  should_validate_presence_of :content
  should_validate_presence_of :article_id

  should_not_allow_values_for :name, "123", "bob_y", "dave!"
  should_allow_values_for :name, "Bob", "Joe Smith"

  should_not_allow_values_for :email, "abc", "[email protected]", "a@!d.com", "[email protected]"
  should_allow_values_for :email, "[email protected]", "[email protected]", "[email protected]"
end


# myapp/app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :article

  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :content
  validates_presence_of :article_id

  validates_format_of :name, :with => /\b[\w]+\b/i
end

我正在其他模型中进行类似的验证,并且效果很好,但在这个模型中我遇到了这些错误:

  2) Failure:
test: Comment should not allow email to be set to "[email protected]". (CommentTest)
[/Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/assertions.rb:67:in `assert_rejects'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/active_record/macros.rb:139:in `__bind_1276100388_113010'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: Comment should not allow email to be set to "[email protected]". ']:
Expected errors when email is set to "[email protected]", got errors: name can't be blank (nil)name is invalid (nil)content can't be blank (nil)article_id can't be blank (nil)

我在应该进行的每个测试中都得到了这个,我是如果重要的话也可以使用工厂女孩。

I am trying to test a model's attributes for formatting with this:

# myapp/test/unit/comment_test.rb
require 'test_helper'

class CommentTest < ActiveSupport::TestCase
  should_belong_to :article

  should_validate_presence_of :name
  should_validate_presence_of :email
  should_validate_presence_of :content
  should_validate_presence_of :article_id

  should_not_allow_values_for :name, "123", "bob_y", "dave!"
  should_allow_values_for :name, "Bob", "Joe Smith"

  should_not_allow_values_for :email, "abc", "[email protected]", "a@!d.com", "[email protected]"
  should_allow_values_for :email, "[email protected]", "[email protected]", "[email protected]"
end


# myapp/app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :article

  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :content
  validates_presence_of :article_id

  validates_format_of :name, :with => /\b[\w]+\b/i
end

I am doing a similar sort of validation in other models and it works fine, but in this one I am getting these errors:

  2) Failure:
test: Comment should not allow email to be set to "[email protected]". (CommentTest)
[/Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/assertions.rb:67:in `assert_rejects'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/active_record/macros.rb:139:in `__bind_1276100388_113010'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: Comment should not allow email to be set to "[email protected]". ']:
Expected errors when email is set to "[email protected]", got errors: name can't be blank (nil)name is invalid (nil)content can't be blank (nil)article_id can't be blank (nil)

I get this on every test shoulda makes, I am using Factory Girl also if that matters.

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

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

发布评论

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

评论(1

樱花落人离去 2024-09-11 13:05:26

您的评论模型未验证电子邮件格式。您所做的事情没有任何问题,您的测试只是没有通过,因为您的代码对于测试通过来说不正确。

将电子邮件格式的验证添加到您的 Comment 类中。

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

然后你的测试应该通过。

Your comment model doesn't validate the email format. There isn't anything wrong with what you are doing, your test just isn't passing because your code isn't correct for the test to pass.

Add a validation for the email format to your Comment class.

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

and then your test should pass.

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