Ruby on rails RSpec测试装置
我是RSpec和TDD的新手,我很难编写RSpec测试来测试Deave是否在用户注册后发送确认电子邮件。我知道我的应用程序正在按预期工作,因为我已经在开发和生产中对功能进行了物理测试。但是,我仍然需要为这个功能编写RSpec测试,我无法确定如何通过RSpec测试发送确认电子邮件Ruby on rails RSpec测试装置,ruby-on-rails,ruby,rspec,devise,factory-bot,Ruby On Rails,Ruby,Rspec,Devise,Factory Bot,我是RSpec和TDD的新手,我很难编写RSpec测试来测试Deave是否在用户注册后发送确认电子邮件。我知道我的应用程序正在按预期工作,因为我已经在开发和生产中对功能进行了物理测试。但是,我仍然需要为这个功能编写RSpec测试,我无法确定如何通过RSpec测试发送确认电子邮件 factories/user.rb FactoryGirl.define do factory :user do name "Jack Sparrow" email { Faker::Internet
factories/user.rb
FactoryGirl.define do
factory :user do
name "Jack Sparrow"
email { Faker::Internet.email }
password "helloworld"
password_confirmation "helloworld"
confirmed_at Time.now
end
end
require 'rails_helper'
RSpec.describe User, type: :model do
describe "user sign up" do
before do
@user = FactoryGirl.create(:user)
end
it "should save a user" do
expect(@user).to be_valid
end
it "should send the user an email" do
expect(ActionMailer::Base.deliveries.count).to eq 1
end
end
end
spec/models/user\u spec.rb
FactoryGirl.define do
factory :user do
name "Jack Sparrow"
email { Faker::Internet.email }
password "helloworld"
password_confirmation "helloworld"
confirmed_at Time.now
end
end
require 'rails_helper'
RSpec.describe User, type: :model do
describe "user sign up" do
before do
@user = FactoryGirl.create(:user)
end
it "should save a user" do
expect(@user).to be_valid
end
it "should send the user an email" do
expect(ActionMailer::Base.deliveries.count).to eq 1
end
end
end
为什么在我创建@user之后,designe不发送确认电子邮件?我的测试返回ActionMailer::Base.deliveries.count=0。正如我所说,我是RSpec和TDD的新手,所以我在这里完全遗漏了什么吗?Deave使用自己的邮箱,因此,如果将测试放在正确的控制器文件中本身不起作用,请尝试
designe.mailer.deliveries
而不是ActionMailer::Base.deliveries
。我猜电子邮件是通过控制器创建操作发送的,这里您只是创建了一个新用户,并希望它发送电子邮件。因此,我将为控制器创建操作编写一个测试,并使用一些用户属性发布一篇文章,mailer应该被调用。因此,我需要在registrations\u controller\u spec.rb
中编写测试?就是这样,只要电子邮件是从thereexpect(designe.mailer.deliveries.count)发送的。到eq 1,谢谢!