纯Ruby rspec测试通过,但未定义方法
我在纯Ruby模型上进行了rspec测试:纯Ruby rspec测试通过,但未定义方法,ruby,rspec,Ruby,Rspec,我在纯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 我的组
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
当我运行测试时,它通过了,尽管方法是_root?模型上不存在。我通常在Rails中工作,而不是在纯Ruby中,我从未见过这种情况发生。发生了什么事
谢谢 应该是:
expect(org.is_root?).to eq true
当您将块传递给expect
时,它被包装在ExpectationTarget
类中(严格来说BlockExpectationTarget
)。由于未指定对此对象的期望值,因此不会执行该块,因此不会引发错误。应该是:
expect(org.is_root?).to eq true
当您将块传递给
expect
时,它被包装在ExpectationTarget
类中(严格来说BlockExpectationTarget
)。由于您没有指定对此对象的期望值,因此不会执行该块,因此不会引发任何错误。您正在传递一个从未被调用的期望值块。通过在该块上设置期望值,可以看到这一点
expect { org.is_root?.to eq true }.to_not raise_error
1) Organization#is_root? creates a root organization
Failure/Error: expect { puts "HI";org.is_root?.to eq true }.to_not raise_error
expected no Exception, got #<NoMethodError: undefined method `is_root?' for #<Organization:0x007ffa798c2ed8 @parent=nil>> with backtrace:
# ./test_spec.rb:15:in `block (4 levels) in <top (required)>'
# ./test_spec.rb:15:in `block (3 levels) in <top (required)>'
# ./test_spec.rb:15:in `block (3 levels) in <top (required)>'
块形式用于期望一段代码是否引发异常。检查值的正确语法为:
expect(org.is_root?).to eq(true)
您正在通过一个预期的块,该块从未被调用。通过在该块上设置期望值,可以看到这一点
expect { org.is_root?.to eq true }.to_not raise_error
1) Organization#is_root? creates a root organization
Failure/Error: expect { puts "HI";org.is_root?.to eq true }.to_not raise_error
expected no Exception, got #<NoMethodError: undefined method `is_root?' for #<Organization:0x007ffa798c2ed8 @parent=nil>> with backtrace:
# ./test_spec.rb:15:in `block (4 levels) in <top (required)>'
# ./test_spec.rb:15:in `block (3 levels) in <top (required)>'
# ./test_spec.rb:15:in `block (3 levels) in <top (required)>'
块形式用于期望一段代码是否引发异常。检查值的正确语法为:
expect(org.is_root?).to eq(true)
在Terminal中运行测试后,您可以发布输出吗?您还可以启动
rails控制台
并询问o.methods.select do | m | m.match/root/end
来验证您关于is_root?
的假设,显然,测试在expect{}中。当我放置org.method(:is_root?)时,我得到一个失败:1)Organization#is_root?为类organization'./spec/organization\u spec.rb:10:in
method'.\spec/organization\u spec.rb:10:in
method'./spec/organization\u spec.rb:10:inblock(3级)in'
Patru,因为这不是Rails应用程序,而是纯Ruby,没有可用的rails控制台。您可以在Terminal中运行测试后发布输出吗?您还可以启动rails控制台
并询问o.methods.select do | m | m.match/root/end
以验证您关于是根吗?
的假设,显然,测试在expect{}中。当我放置org.method(:is_root?)时,我得到一个失败:1)Organization#is_root?为类organization'./spec/organization\u spec.rb:10:in
method'.\spec/organization\u spec.rb:10:in
method'./spec/organization\u spec.rb:10:inblock(3级)in'
Patru,因为这不是Rails应用程序,而是纯Ruby,没有可用的rails控制台。