这是评论-sspec,它有效,但我不明白双重做什么。我的研究一无所获。
要求' rails_helper'
describe Comment do
include TestFactories
describe "after_create" do
before do
@post = associated_post
@user = authenticated_user
@comment = Comment.new(body: 'My comment', post: @post, user_id: 10000)
end
it "sends an email to users who have favorited the post" do
@user.favorites.where(post: @post).create
allow( FavoriteMailer )
.to receive(:new_comment)
.with(@user, @post, @comment)
.and_return( double(deliver_now: true) )
@comment.save
end
it "does not send emails to users who haven't" do
expect( FavoriteMailer )
.not_to receive(:new_comment)
@comment.save
end
end
end
答案 0 :(得分:0)
double是RSpec中的基本模拟对象,如https://relishapp.com/rspec/rspec-mocks/docs中所述。在您的情况下,它提供了deliver_now
方法的实现。但是,你的第一个it
块并没有真正测试任何东西,因为其中没有断言。