【问题标题】:How do I access sinatra settings in custom extension specs?如何访问自定义扩展规范中的 sinatra 设置?
【发布时间】:2014-05-08 11:16:00
【问题描述】:

我为我的应用程序编写了一个电子邮件助手扩展程序。我使用我在应用程序上定义的设置,如下所示:

set :mailgun_options, JSON.parse(File.open('config/mailer.json').read)[ENV['RACK_ENV']]

它将配置设置从 json 文件读取到全局 Sinatra 设置中。

然后我有我的电子邮件助手,它引用这些设置来创建连接等等。

require 'sinatra/base'
require 'faraday'

module Sinatra
    module EmailHelper
        def connect opts={}
            connection =  Faraday.new(:url => settings.mailgun_options['mailgun_url']) do |faraday|
                if settings.mailgun_options['test']
                    faraday.adapter :test do |stubs|
                        stubs.post(settings.mailgun_options['domain'] + '/messages') {[ 200, {}, 'success' ]}
                    end
                else
                    faraday.request :url_encoded
                    faraday.adapter Faraday.default_adapter
                end
            end

            connection.basic_auth('api', settings.mailgun_options['key'])
            return connection
        end

        def send_email params={}
            connect.post((settings.mailgun_options['domain'] + '/messages'), params)
        end

        def send_password_reset_email email
            template = File.open( File.expand_path( '../../helpers/email_helper/templates/password_reset.erb', __FILE__ ), 'r' ).read

            send_email({
                :from => 'REscour <noreply@' + settings.mailgun_options['domain'] + '>',
                :to => email,
                :subject => 'REscour.com Password Reset',
                :text => ERB.new(template).result(binding)
            })
        end
    end
end

因此,当我尝试编写规范以测试“连接”方法时,我收到以下错误:

undefined local variable or method `settings' for #<Class:0x007faaf9c9bd18>

我的规范代码如下:

module Sinatra
    describe EmailHelper
        let(:dummy_class) { Class.new { extend EmailHelper } }

        it 'should connect to mailgun\'s api' do
            app.set :mailgun_options, ::JSON.parse(File.open('config/mailer.json').read)['test']
            puts dummy_class.connect
        end
    end
end

我不确定如何与我的规范中的全局 Sinatra 设置进行交互,以便在执行帮助程序代码期间定义本地“设置”变量。

据我所知,我的应用程序架构可能在这里出错。有没有人有使用全局设置编写 Sinatra 扩展并为其编写规范的经验?

【问题讨论】:

    标签: unit-testing rspec sinatra


    【解决方案1】:

    您编写的规范似乎想要成为单元测试,因为您还没有run the extension the way it would be used in an app。在规范中,尝试类似:

    describe "connect" do
      let(:app) { Sinatra.new do
                    helpers Sinatra::EmailHelper
    
                    configure do
                      :mailgun_options, ::JSON.parse(File.open('config/mailer.json')
                    end
    
                    get "/" do
                      connect
                    end
                  end
                }
       # now write specs for the `connect` helper
     end
    

    使用匿名类 (Sinatra.new do...) 可以让您快速设置一个便于小型测试的应用,但也有其他方法。

    在扩展中,需要添加:

    helpers EmailHelper
    

    根据the example in the docs

    【讨论】:

    • 我已将助手 EmailHelper 添加到我的助手中,并且我已根据您显示的内容重写了规范。但现在我得到了#<0x007f96391d87c0>
    猜你喜欢
    • 1970-01-01
    • 2019-08-24
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2012-07-08
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多