为条件if语句编写rspec测试未返回任何示例



我是RSPEC的新手,正在尝试为以下功能编写测试:

private
def state_email
if CurrentXPX.is?(:Ohio)
:oh_update_reminder
elsif CurrentXPX.is?(:North_Carolina)
:nc_update_reminder
else CurrentXPX.is?(:Missouri)
:mo_update_reminder
end
end

`CurrentXPX.is?返回状态缩写的函数如下所示:

module CurrentXPX
class << self
def is?(state)
s = State::Abbreviations.const_get state
return state_abbreviation == s
end

":oh_update_ereminder"函数返回以下语句:

class AccountUpdateReminder < ActionMailer::Base
def oh_update_reminder(email)
mail(to: email, subject: AccountUpdateReminder.subject)
end
def nc_update_reminder(email)
mail(to: email, subject: AccountUpdateReminder.subject)
end
def mo_update_reminder(email)
mail(to: email, subject: AccountUpdateReminder.subject)
end
end

最后,我写下了这样一个测试:

describe 'state_email' do

context 'ohio state email' do
it 'send ohio update reminder' do
if (CurrentXPX.is?("Ohio"))
let(:oh_update_reminder){ mail(to: email, subject: AccountUpdateReminder.subject)}
expect(:oh_update_reminder).to be_truthy
end
end
end
context 'North Carolina state email' do
it 'send north carolina update reminder' do
if (CurrentXPX.is?("North_Carolina"))
let(:nc_update_reminder){ mail(to: email, subject: AccountUpdateReminder.subject)}
expect(:nc_update_reminder).to be_truthy
end
end
end
context 'Missouri state email' do
it 'send Missouri update reminder' do
if (CurrentXPX.is?("Missouri"))
let(:mo_update_reminder){ mail(to: email, subject: AccountUpdateReminder.subject)}
expect(:mo_update_reminder).to be_truthy
end
end
end
end

当运行rspec测试时,我得到了:

LoadError: cannot load such file -- blah blah blah
No examples found.
Top 0 slowest examples (0 seconds, 0.0% of total time):
Finished in 0.00003 seconds (files took 0.6038 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

我非常怀疑我的Rspec测试不正确,但它也可能更正确。我很感激能得到的任何帮助/提示。

您似乎缺少一个subject声明并在示例中调用它,因此rspec实际上并没有测试任何内容。

你想测试方法#state_email吗?在这种情况下,您需要

describe '#state_email' do
subject(:state_email) { described_class.new.state_email }

it "is [your criteria here]" do
expect(state_email).to be_present # your test here
end
end

或者您正在尝试测试AccountUpdateReminder中的方法?

RSpec.describe AccountUpdateReminder do
describe "#oh_update_reminder" do
let(:email) { "test@test.com" }
subject(:oh_update_reminder) { described_class.new.oh_update_reminder(email) }
it "mails" do
expect(described_class).to receive(:mail).with(to: email)
oh_update_reminder
end
end
end

最新更新