为什么 rspec 没有在模型中应该是 NameError 的地方失败?

Why isn't rspec failing on what should be a NameError inside a Model?

我有一个 rspec 看起来像:

it "should test nil" do
  u = Upload.new
  u.save!
  u.my_method!
end

该方法如下所示:

class Upload < ActiveRecord::Base
  def my_method!
    puts "Made it to my method :)"
    not_defined_var
    puts "But didn't get this far :("
  end
end

显然,not_defined_var 应该给出 "NameError: undefined local variable or method"。

我的输出打印

Made it to my method :)
.

Finished in 1.54 seconds
1 example, 0 failures

我觉得我缺少一些重要的东西。有什么想法吗?

您没有调用 expect 方法,所以 Rspec 只是传递。要比较是否按预期进行,您必须调用它。要测试代码,您必须将响应与您的预期进行比较。

示例:

RSpec.describe Account do
  it "has a balance of zero when first created" do
    expect(Account.new.balance).to eq(Money.new(0))
  end
end

https://relishapp.com/rspec/rspec-expectations/v/3-3/docs