Ruby on rails 用rspec测试sendus_ruby

Ruby on rails 用rspec测试sendus_ruby,ruby-on-rails,rspec,sendwithus,Ruby On Rails,Rspec,Sendwithus,我们使用sendwithus ruby gem在Rails应用程序中发送电子邮件。(). 如何使用rspec测试发送电子邮件?嗯,我知道这里有三个选项-哪一个最好取决于您测试的内容以及测试环境的设置 使用rspec mock拦截Sendwithus API调用,并在mock中执行您自己的验证 使用网络捕获库(如VCR)捕获Sendwithus gem发出的API调用。然后,您可以验证并断言捕获的请求与您期望的一样 使用Sendwithus测试API密钥并实际对Sendwithus帐户进行API调

我们使用sendwithus ruby gem在Rails应用程序中发送电子邮件。(). 如何使用rspec测试发送电子邮件?

嗯,我知道这里有三个选项-哪一个最好取决于您测试的内容以及测试环境的设置

  • 使用rspec mock拦截Sendwithus API调用,并在mock中执行您自己的验证

  • 使用网络捕获库(如VCR)捕获Sendwithus gem发出的API调用。然后,您可以验证并断言捕获的请求与您期望的一样

  • 使用Sendwithus测试API密钥并实际对Sendwithus帐户进行API调用。可以将测试API密钥配置为从不发送电子邮件,或将所有电子邮件转发到固定的电子邮件地址。更多信息请点击此处:


  • 嗯,我知道这里有三个选项-哪一个是最好的将取决于你到底测试什么以及你的测试环境是如何设置的

  • 使用rspec mock拦截Sendwithus API调用,并在mock中执行您自己的验证

  • 使用网络捕获库(如VCR)捕获Sendwithus gem发出的API调用。然后,您可以验证并断言捕获的请求与您期望的一样

  • 使用Sendwithus测试API密钥并实际对Sendwithus帐户进行API调用。可以将测试API密钥配置为从不发送电子邮件,或将所有电子邮件转发到固定的电子邮件地址。更多信息请点击此处:


  • 这是一个使用vcr库的测试。不漂亮,但很管用。分享你对如何改进它的想法

    用于测试的Ruby包装器:

    class TestWithUs
    
      CASSETTES_PATH = 'fixtures/vcr_cassettes/'
    
      def initialize(name)
        @name = name
        @cassette_file = get_cassette_file(name)
      end
    
      def track(&block)
        File.delete(@cassette_file) if File.exist?(@cassette_file)
        VCR.use_cassette(@name) do
          block.call
        end
      end
    
      def results
        YAML.load(File.read @cassette_file)["http_interactions"]
      end
    
      private
    
      def get_cassette_file(name)
        CASSETTES_PATH + name + ".yml"
      end
    
    end
    
    测试文件:

    require 'spec_helper'
    require 'vcr'
    
    VCR.configure do |config|
      config.cassette_library_dir = "fixtures/vcr_cassettes"
      config.hook_into :webmock
      #config.ignore_request { |r| r.uri =~ /localhost:9200/ }
      config.ignore_localhost = true
    end
    
    describe 'messages sent to matt' do
      before do
        @test_with_us = TestWithUs.new("welcome_email")
        @test_with_us.track do
    
          # Usually it sends email on some kind of callback,
          # but for this example, it's straightforward
    
          SENDWITHUS.send_with(CONFIG.swu_emails[:welcome],
            { address: "user@example.com" },
            {company_name: 'Meow Corp'})
        end
      end
    
      it "Sends an email" do
        sendwithus_calls = @test_with_us.results.select {|c| c["request"]["uri"] == "https://api.sendwithus.com/api/v1/send"}
        expect(sendwithus_calls.count).to eq(1)
      end
    end
    

    这是一个使用vcr库的测试。不漂亮,但很管用。分享你对如何改进它的想法

    用于测试的Ruby包装器:

    class TestWithUs
    
      CASSETTES_PATH = 'fixtures/vcr_cassettes/'
    
      def initialize(name)
        @name = name
        @cassette_file = get_cassette_file(name)
      end
    
      def track(&block)
        File.delete(@cassette_file) if File.exist?(@cassette_file)
        VCR.use_cassette(@name) do
          block.call
        end
      end
    
      def results
        YAML.load(File.read @cassette_file)["http_interactions"]
      end
    
      private
    
      def get_cassette_file(name)
        CASSETTES_PATH + name + ".yml"
      end
    
    end
    
    测试文件:

    require 'spec_helper'
    require 'vcr'
    
    VCR.configure do |config|
      config.cassette_library_dir = "fixtures/vcr_cassettes"
      config.hook_into :webmock
      #config.ignore_request { |r| r.uri =~ /localhost:9200/ }
      config.ignore_localhost = true
    end
    
    describe 'messages sent to matt' do
      before do
        @test_with_us = TestWithUs.new("welcome_email")
        @test_with_us.track do
    
          # Usually it sends email on some kind of callback,
          # but for this example, it's straightforward
    
          SENDWITHUS.send_with(CONFIG.swu_emails[:welcome],
            { address: "user@example.com" },
            {company_name: 'Meow Corp'})
        end
      end
    
      it "Sends an email" do
        sendwithus_calls = @test_with_us.results.select {|c| c["request"]["uri"] == "https://api.sendwithus.com/api/v1/send"}
        expect(sendwithus_calls.count).to eq(1)
      end
    end
    

    这看起来是一个非常明智的解决方案!:)这看起来是一个非常明智的解决方案!:)