rspec - 为什么 be_valid 不起作用

发布于 2024-10-25 11:16:52 字数 2130 浏览 0 评论 0原文

我正在尝试执行此操作:

  describe "should fail on create if the name is shorter than 5 characters" do
    group = Factory.build(:group, :name => "a")
    group.should be_invalid
    group.should have(1).error_on(:name)
  end

但出现以下错误:

/spec/models/group_model_spec.rb:22: undefined local variable or method `be_valid' for #<Class:0x105d13ed0> (NameError)
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `module_eval'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `subclass'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:119:in `describe'
    from /xxxx/spec/models/group_model_spec.rb:15
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `module_eval'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `subclass'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:119:in `describe'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/extensions/object.rb:6:in `describe'
    from /Users/bhellman/Sites/companyline/spec/models/group_model_spec.rb:3
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `autorun'
    from /usr/bin/rspec:19

有想法吗?谢谢

I'm trying to do this:

  describe "should fail on create if the name is shorter than 5 characters" do
    group = Factory.build(:group, :name => "a")
    group.should be_invalid
    group.should have(1).error_on(:name)
  end

But I get the following error:

/spec/models/group_model_spec.rb:22: undefined local variable or method `be_valid' for #<Class:0x105d13ed0> (NameError)
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `module_eval'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `subclass'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:119:in `describe'
    from /xxxx/spec/models/group_model_spec.rb:15
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `module_eval'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:132:in `subclass'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:119:in `describe'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/extensions/object.rb:6:in `describe'
    from /Users/bhellman/Sites/companyline/spec/models/group_model_spec.rb:3
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `autorun'
    from /usr/bin/rspec:19

Ideas? Thanks

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

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

发布评论

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

评论(1

少女七分熟 2024-11-01 11:16:52

这是正确的语法:

group.should_not be_valid

This is the correct syntax :

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