We've moved discussions to Discord

Test error when Braintree payment is enabled?

Aska Konomi
Hi, I got these errors when running rails test after enabled Braintree (and PayPal) to my app:

INFO -- : [Braintree] [23/Jan/2022 08:51:26 UTC] POST /merchants//client_token 404

Error:
PaymentMethodsTest#test_fake_processor_sees_a_message:
ActionView::Template::Error: No more expects available for :client_token: []
    app/views/payment_methods/forms/_braintree.html.erb:6
    app/views/payment_methods/new.html.erb:19
    lib/jumpstart/lib/jumpstart/account_middleware.rb:30:in `call'
    test/integration/payment_methods_test.rb:18:in `block in <class:PaymentMethodsTest>'


rails test test/integration/payment_methods_test.rb:16

INFO -- : [Braintree] [23/Jan/2022 08:21:41 UTC] POST /merchants//client_token 404

Error:
PaymentMethodsTest#test_user_can_add_a_payment_method_without_a_processor_set:
ActionView::Template::Error: Braintree::NotFoundError
    app/views/payment_methods/forms/_braintree.html.erb:6
    app/views/payment_methods/new.html.erb:19
    lib/jumpstart/lib/jumpstart/account_middleware.rb:30:in `call'
    test/integration/payment_methods_test.rb:12:in `block in <class:PaymentMethodsTest>'


rails test test/integration/payment_methods_test.rb:11

INFO -- : [Braintree] [23/Jan/2022 08:51:26 UTC] POST /merchants//client_token 404

Error:
PurchasesControllerTest#test_should_get_new:
ActionView::Template::Error: Braintree::NotFoundError
    app/views/purchases/_form.html.erb:6
    app/views/purchases/new.html.erb:23
    lib/jumpstart/lib/jumpstart/account_middleware.rb:30:in `call'
    test/controllers/purchases_controller_test.rb:15:in `block in <class:PurchasesControllerTest>'


rails test test/controllers/purchases_controller_test.rb:14

I guess the `POST /merchants//client_token 404` is the issue, but I have no idea how to fix it.

The actual payment page and Braintree/PayPal integration is working, at least in the sandbox environment.
But the tests shipped with JumpstartPro seemed to fail. Any help?
I already added the credentials to credentials/test.

Update:

It seems this part is causing the issue:
<%= form_with url: local_assigns.fetch(:url, subscriptions_path),
  method: local_assigns.fetch(:method, :post),
  data: {
    controller: "braintree",
    braintree_target: "form",
    braintree_env: braintree_env,
    braintree_client_token: Pay.braintree_gateway.client_token.generate,
  } do |f| %>
More specific, it seems Pay.braintree_gateway.client_token.generate is not working in test environment. Any ideas?
Aska Konomi
Update #2:

After I added the actual payment_processor code from the Pay gem documentation to my controller, the payment worked, but I got a new error when running the test:
Error:
PurchasesControllerTest#test_should_create_purchase:
Pay::Braintree::Error: Braintree::NotFoundError
    app/controllers/purchases_controller.rb:54:in `create'
    lib/jumpstart/lib/jumpstart/account_middleware.rb:30:in `call'
    test/controllers/purchases_controller_test.rb:21:in `block (2 levels) in <class:PurchasesControllerTest>'
    test/controllers/purchases_controller_test.rb:20:in `block in <class:PurchasesControllerTest>'


rails test test/controllers/purchases_controller_test.rb:19

And this is the create code in my controller:
def create
  @purchase = Purchase.new(purchase_params)
  authorize @purchase

  # set payment processor when necessary
  account = current_account
  account.set_payment_processor :braintree if current_account.payment_processor.blank?

  account.payment_processor.payment_method_token = params[:payment_method_token]
  account.payment_processor.charge(@purchase.price_total_cents)

  respond_to do |format|
    if @purchase.save
      format.html { redirect_to @purchase, notice: "Purchase info was successfully created." }
      format.json { render :show, status: :created, location: @purchase }
    else
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @purchase.errors, status: :unprocessable_entity }
    end
  end
end

The test content is from the scaffold:
test "should create purchase" do
  assert_difference("Purchase.count") do
    post purchases_url, params: {purchase: {account_id: @purchase.account_id, payment_data: @purchase.payment_data, payment_method: @purchase.payment_method, price_total: @purchase.price_total}}
  end

  assert_redirected_to purchase_url(Purchase.last)
end

After looking at the test code, I found there is a helper for Stripe: StripeSystemTestHelper for filling out the payment form, but there isn't one for Braintree.
So I guess the test is just not complete with Braintree?
Notifications
You’re not receiving notifications from this thread.