gpt4 book ai didi

ruby-on-rails - Rspec: ActionMailer::Base.deliveries 应该是空的

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:24 26 4
gpt4 key购买 nike

测试在一个测试中填写联系表而不填写必填字段,然后在另一个测试中填写 honey_pot 字段。填写有效信息时测试通过,但其他两个规范失败。

更新 我已经运行了几次测试,有时所有测试都通过了。其他时候,相同的规范会失败。

终端输出

  3) Pages Home page it should behave like all static pages Footer Contact Form when honeypot is filled does not send an email
Failure/Error: expect(ActionMailer::Base.deliveries).to be_empty
expected empty? to return true, got false
Shared Example Group: "all static pages" called from ./spec/requests/pages_spec.rb:69
# ./spec/requests/pages_spec.rb:46:in `block (6 levels) in <top (required)>'

4) Pages Home page it should behave like all static pages Footer Contact Form when fields are not filled does not send an email
Failure/Error: expect(ActionMailer::Base.deliveries).to be_empty
expected empty? to return true, got false
Shared Example Group: "all static pages" called from ./spec/requests/pages_spec.rb:69
# ./spec/requests/pages_spec.rb:38:in `block (6 levels) in <top (required)>'

pages_spec.rb

require 'spec_helper'                                                                                                                                    

describe "Pages" do

subject { page }

shared_examples_for "all static pages" do |path_name|
before { visit send(path_name) }

describe "Footer" do

describe "Contact Form" do
it { should have_selector('h7', text: 'Contact Us') }

context "when a valid message" do

it "sends an email" do
post contact_create_path, message: attributes_for(:message)
expect(ActionMailer::Base.deliveries.last.to).to eq(["#{ENV["MVP_USERNAME"]}"])
ActionMailer::Base.deliveries.clear
end
end

context "when fields are not filled" do
it "does not send an email" do
post contact_create_path, message: attributes_for(:message, name: '', body: '')
expect(ActionMailer::Base.deliveries).to be_empty
ActionMailer::Base.deliveries.clear
end
end
end

context "when honeypot is filled" do
it "does not send an email" do
post contact_create_path, message: attributes_for(:message, sweet_honey: 'bot')
expect(ActionMailer::Base.deliveries).to be_empty
ActionMailer::Base.deliveries.clear
end
end
end
end
end

describe "Home page" do
before { visit root_path }
it_should_behave_like "all static pages", :root_path
it { should have_text('Quality Cars') }
it { should have_title('Misawa Used Cars - Misawa Auto Sales') }

describe "Send a message" do
before do
fill_in "Name", with: 'name'
fill_in "Email", with: 'email@example.com'
fill_in "Phone", with: '999-9999-9999'
fill_in "Body", with: 'Hello'
click_button "Send"
end

describe "after the message is sent" do

it "should render the desired page with a flash" do
expect(page).to have_text('Quality Cars')
expect(page).to have_title('Misawa Used Cars - Misawa Auto Sales')
expect(page).to have_selector('.alert-box.success')
end
end
end
end
end

最佳答案

通过在我测试交付的每个规范之前和之后清除 ActionMailer::Base.deliveries,我能够让规范通过。我还使用 Rspec's documentation 中描述的 before(:each)after(:each) 方法重写了测试,使其更干燥. 更新 更好的是,around(:each)

改进的规范

  describe "Contact Form" do                                                                                                                         
it { should have_selector('h7', text: 'Contact Us') }

describe "send a message" do
around(:each) { ActionMailer::Base.deliveries.clear }

context "when a valid message" do
it "sends an email" do
post contact_create_path, message: attributes_for(:message)
expect(ActionMailer::Base.deliveries.last.to).to eq(["#{ENV["MVP_USERNAME"]}"])
end
end

context "when fields are not filled" do
it "does not send an email" do
post contact_create_path, message: attributes_for(:message, name: '', body: '')
expect(ActionMailer::Base.deliveries).to be_empty
end
end

context "when honeypot is filled" do
it "does not send an email" do
post contact_create_path, message: attributes_for(:message, sweet_honey: 'bot')
expect(ActionMailer::Base.deliveries).to be_empty
end
end
end
end

关于ruby-on-rails - Rspec: ActionMailer::Base.deliveries 应该是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21423779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com