レールのメーラーでは、すべてのメソッドがクラスメソッドになることはわかっています。 しかし、私はと呼ばれる自分のメーラーメソッドをテストすることはできません。rspecのレールで呼び出されるメーラーの動作をテストできません
user_mailer_spec.rb:
it "should call send_notifition method" do
@user = FactoryGirl.build(:user)
notify_email = double(:send_notifition)
expect(UsersMailer.new).to receive(:notify_email).with(@user)
@user.save
end
user_mailer.rb:
def notify(user)
mail to: user.email, subject: "Example"
end
user.rb:
after_commit :send_notifition
private
def send_notifition
UsersMailer.notify(self)
end
上記のコードは合格しませんが、私がself.notifitionへの通知を変更すると、それは次のようになります:
def self.notify(user)
mail to: user.email, subject: "Example"
end
申し訳ありませんが、間違い。私は「expect(UsersMailer).to(:notify_email).with(@user)」を受け取りますが、動作しなくなるまでです。 – Hung
私はあなたの提案を試みたが、合格しないまでです。 – Hung
私が言及したようにコールを修正しましたか?私。コールバックは 'UsersMailer.notify(self) 'でなければなりません。deliver_now'(または、バックグラウンドワーカーを使用する場合は 'deliver_later'、古いRailsバージョンを使用している場合は' deliver'のみ)を使用します。 –