我有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]
。
答案 0 :(得分:0)
我会将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