rspec - why does be_valid not work

房东的猫 提交于 2019-12-10 10:14:01

问题


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


回答1:


This is the correct syntax :

group.should_not be_valid


来源:https://stackoverflow.com/questions/5384716/rspec-why-does-be-valid-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!