【发布时间】:2014-05-20 09:34:21
【问题描述】:
我对纯 Ruby 模型进行了 rspec 测试:
require 'spec_helper'
require 'organization'
describe Organization do
context '#is_root?' do
it "creates a root organization" do
org = Organization.new
expect { org.is_root?.to eq true }
end
end
end
我的组织模型如下所示:
class Organization
attr_accessor :parent
def initialize(parent = nil)
self.parent = parent
end
end
运行测试时的输出:
bundle exec rspec spec/organization_spec.rb:6
Run options: include {:locations=>{"./spec/organization_spec.rb"=>[6]}}
.
Finished in 0.00051 seconds
1 example, 0 failures
当我运行测试时,它通过了,尽管方法 is_root?模型上不存在。我通常在 Rails 中工作,而不是纯 Ruby,而且我从未见过这种情况发生。怎么回事?
谢谢!
【问题讨论】:
-
你可以在终端运行测试后发布输出
-
您也可以发起
rails console并询问o.methods.select do |m| m.match /root/ end以验证您对is_root?的假设 -
显然它在 expect{} 内进行了测试。当我放 org.method(:is_root?) 我失败了:
1) Organization#is_root? creates a root organization Failure/Error: org.method(:is_root?) NameError: undefined methodis_root?'对于类Organization' # ./spec/organization_spec.rb:10:inmethod' # ./spec/organization_spec.rb:10:inblock (3 levels) in <top (required)>' -
Patru,因为这不是 Rails 应用程序,而是纯 Ruby,所以没有可用的 Rails 控制台。