【问题标题】:How to test controller helper statement?如何测试控制器辅助语句?
【发布时间】:2016-04-26 18:02:20
【问题描述】:

我有checkouts_controller.rb 行动:

def create
    if create_customer(params[:stripeToken], @plan.my_plan_id, params[:code], params[:new_price])
      redirect_to user_path(id: current_user.to_param, customer_share: true)
    else
      redirect_to new_checkout_path(plan_id: @plan)
    end
end

users_helper.rb:

def create_customer
if customer != nil
...
else
flash[:error] = 'Something went wrong, please try again.'
end

所以我需要检查 else 并在 rspec 中测试 flash[:error]

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    我会将flash 作为我的create_customer 的参数传递给make the code testable。这也符合 OOP 原则。

    但是,如果您真的必须按原样测试代码,那么:

    module UsersHelper
      def flash
        @flash ||= {}
      end
    end
    
    describe UsersHelper do
      subject { Object.new.extend(described_class) }
    
      context 'Customer does not exist' do
        it 'sets flash mesage' do
          # your test code
        end
      end
    end
    

    【讨论】:

    • 触发 Stripe::InvalidRequestError
    • 显然你正在使用stripe-ruby gem。您将满足所有与条纹相关的要求。
    猜你喜欢
    • 2011-08-03
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2011-06-07
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多