RSpec 模型验证失败但在 Rails Console 中工作正常?

发布于 2024-11-17 08:34:50 字数 2618 浏览 3 评论 0原文

我真的一直被这个问题困扰。当我在 Rails 控制台中测试模型时,我对模型的验证失败了(应该如此),但我的 rspec 示例失败了,因为它无论如何都会验证(它不应该验证)。我在这里缺少什么或者我应该从哪里开始寻找?

谢谢, 伊万

见下图:

    ivan:~/Development/ruby/IvanEnviromanRefinery [git:master+]  → bundle exec rspec ../refinerycms-link/
    .............................F.

    Failures:

      1) BlogPost bookmark links must have a link url if it is a link
         Failure/Error: Factory.build(:post,
           expected valid? to return false, got true
         # /Users/ivan/Development/ruby/refinerycms-link/spec/models/blog_post_spec.rb:197:in `block (3 levels) in <top (required)>'

    Finished in 6.56 seconds
    31 examples, 1 failure

    Failed examples:

    rspec /Users/ivan/Development/ruby/refinerycms-link/spec/models/blog_post_spec.rb:196 # BlogPost bookmark links must have a link url if it is a link


    ivan:~/Development/ruby/IvanEnviromanRefinery [git:master+]  → rails console
    Loading development environment (Rails 3.0.9)
    ruby-1.9.2-p180 :001 > b = BlogPost.new
     => #<BlogPost id: nil, title: nil, body: nil, draft: nil, published_at: nil, created_at: nil, updated_at: nil, user_id: nil, cached_slug: nil, custom_url: nil, link_url: nil, is_link: nil, custom_teaser: nil> 
    ruby-1.9.2-p180 :002 > b.title=' 2011-06-24 8:34 '
     => " 2011-06-24 8:34 " 
    ruby-1.9.2-p180 :003 > b.body='goo'
     => "goo" 
    ruby-1.9.2-p180 :004 > b.is_link = true
     => true 
    ruby-1.9.2-p180 :005 > b.valid?
     => false
    ruby-1.9.2-p180 :007 > b.save
     => false 
    ruby-1.9.2-p180 :008 > b.errors
     => {:link_url=>["must have a link_url if is_link is true"]} 
    ruby-1.9.2-p180 :009 > 


    require 'spec_helper'
    Dir[File.expand_path('../../../features/support/factories/*.rb', __FILE__)].each{|factory| require factory}

    describe BlogPost do
      let(:blog_post ) { Factory :post }

      ...

        describe "bookmark links" do
          it "may have a bookmark link" do
           Factory.build(:post,
                        :title => "a link",
                        :body => "link text",
                        :is_link => true,
                        :link_url => "http://example.com").should be_valid
           end
          it "must have a link url if it is a link" do
            Factory.build(:post,
                                 :title => "a link",
                                 :body => "link text",
                                 :is_link => true).should_not be_valid
          end     
        end
    end

I have been really stuck on this problem. My validation for my model fails (like it should) when I'm testing it out in rails console, but my rspec example fails because it validates anyway (which it shouldn't). What am I missing here or where should I start looking?

Thanks,
Ivan

See below:

    ivan:~/Development/ruby/IvanEnviromanRefinery [git:master+]  → bundle exec rspec ../refinerycms-link/
    .............................F.

    Failures:

      1) BlogPost bookmark links must have a link url if it is a link
         Failure/Error: Factory.build(:post,
           expected valid? to return false, got true
         # /Users/ivan/Development/ruby/refinerycms-link/spec/models/blog_post_spec.rb:197:in `block (3 levels) in <top (required)>'

    Finished in 6.56 seconds
    31 examples, 1 failure

    Failed examples:

    rspec /Users/ivan/Development/ruby/refinerycms-link/spec/models/blog_post_spec.rb:196 # BlogPost bookmark links must have a link url if it is a link


    ivan:~/Development/ruby/IvanEnviromanRefinery [git:master+]  → rails console
    Loading development environment (Rails 3.0.9)
    ruby-1.9.2-p180 :001 > b = BlogPost.new
     => #<BlogPost id: nil, title: nil, body: nil, draft: nil, published_at: nil, created_at: nil, updated_at: nil, user_id: nil, cached_slug: nil, custom_url: nil, link_url: nil, is_link: nil, custom_teaser: nil> 
    ruby-1.9.2-p180 :002 > b.title=' 2011-06-24 8:34 '
     => " 2011-06-24 8:34 " 
    ruby-1.9.2-p180 :003 > b.body='goo'
     => "goo" 
    ruby-1.9.2-p180 :004 > b.is_link = true
     => true 
    ruby-1.9.2-p180 :005 > b.valid?
     => false
    ruby-1.9.2-p180 :007 > b.save
     => false 
    ruby-1.9.2-p180 :008 > b.errors
     => {:link_url=>["must have a link_url if is_link is true"]} 
    ruby-1.9.2-p180 :009 > 


    require 'spec_helper'
    Dir[File.expand_path('../../../features/support/factories/*.rb', __FILE__)].each{|factory| require factory}

    describe BlogPost do
      let(:blog_post ) { Factory :post }

      ...

        describe "bookmark links" do
          it "may have a bookmark link" do
           Factory.build(:post,
                        :title => "a link",
                        :body => "link text",
                        :is_link => true,
                        :link_url => "http://example.com").should be_valid
           end
          it "must have a link url if it is a link" do
            Factory.build(:post,
                                 :title => "a link",
                                 :body => "link text",
                                 :is_link => true).should_not be_valid
          end     
        end
    end

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

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

发布评论

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

评论(1

山川志 2024-11-24 08:34:50

您确定在您的第二个期望中 link_url 没有以某种方式设置吗?尝试输出该值并检查日志

it "must have a link url if it is a link" do
  post = Factory.build(:post,
   :title => "a link",
   :body => "link text",
   :is_link => true)
  Rails.logger.info "Post link: #{post.link_url}"
  post.should_not be_valid
end   

Are you certain that in your second expectation the link_url isnt set somehow? Try outputting the value and check the logs

it "must have a link url if it is a link" do
  post = Factory.build(:post,
   :title => "a link",
   :body => "link text",
   :is_link => true)
  Rails.logger.info "Post link: #{post.link_url}"
  post.should_not be_valid
end   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文