【问题标题】:phpunit reflectionexception error on MocksMocks上的phpunit反射异常错误
【发布时间】:2013-10-28 06:34:46
【问题描述】:

我一直在关注 phpUnit 的教程,但我似乎无法让它工作。

这是链接:

https://jtreminio.com/2013/03/unit-testing-tutorial-part-4-mock-objects-stub-methods-dependency-injection/

There was 1 error:

1) phpUnitTutorial\Test\PaymentTest::testProcessPaymentReturnsTrueOnSuccess
ReflectionException: Class Mock_AuthorizeNetAIM_2d93f549 does not have a constructor, so     you cannot pass any constructor arguments

phpUnitTutorial/Payment.php

<?php

namespace phpUnitTutorial;

class Payment
{
    const API_ID = 123456;
    const TRANS_KEY = 'TRANSACTION KEY';

    //Use Dependency Injection
    public function processPayment(\AuthorizeNetAIM $transaction, array $paymentDetails)
    {
        $transaction->amount = $paymentDetails['amount'];
        $transaction->card_num = $paymentDetails['card_num'];
        $transaction->exp_date = $paymentDetails['exp_date'];

        $response = $transaction->authorizeAndCapture();

        if ($response->approved) {
            return $this->savePayment($response->transaction_id);
        }

        throw new \Exception($response->error_message);
    }

    public function savePayment($transactionId)
    {
        // Logic for saving transaction ID to database or anywhere else would go in here
        return true;
    }
}

phpUnitTutorial/Test/PaymentTest.php

<?php

namespace phpUnitTutorial\Test;

require_once __DIR__ . '/../Payment.php';
use phpUnitTutorial\Payment;

class PaymentTest extends \PHPUnit_Framework_TestCase
{

    public function testProcessPaymentReturnsTrueOnSuccess()
    {
        $paymentDetails = array(
            'amount' => 123.99,
            'card_num' => '4111-111-111-111',
            'exp_date' => '03/2013'
        );

        $payment = new Payment();

        // Note stdClass is used to conveniently create a generic
        // class
        $response = new \stdClass();
        $response->approved = true;
        $response->transaction_id = 123;

        $authorizeNet = $this->getMockBuilder('\AuthorizeNetAIM')
             ->setConstructorArgs(
                 array(
                     $payment::API_ID,
                     $payment::TRANS_KEY
                 )
             )->getMock();

        // $authorizeNet =  $this
        //     ->getMock(
        //         '\AuthorizeNetAIM',
        //         array(),
        //         array($payment::API_ID, $payment::TRANS_KEY)
        //     );

        $authorizeNet->expects($this->once())
            ->method('authorizeAndCapture')
            ->will($this->returnValue($response));

        $result = $payment->processPayment($authorizeNet, $paymentDetails);

        $this->assertTrue($result);
    }
}

【问题讨论】:

标签: php unit-testing phpunit


【解决方案1】:

\AuthorizeNetAIM 没有构造函数 - 如错误消息所述。所以你不能向它传递任何参数。以这种方式创建模拟:

$authorizeNet = $this->getMockBuilder('\AuthorizeNetAIM')->getMock();

或更短:

$authorizeNet = $this->getMock('\AuthorizeNetAIM');

【讨论】:

  • 是的,这就是我一直在想的,但似乎有一个构造函数,因为在我的链接上的教程中,作者用 2 个参数实例化了它。这是教程中的 sn-p。 $transaction = new \AuthorizeNetAIM(self::API_ID, self::TRANS_KEY);
  • 你能指出这个 sn-p 在文档中的哪个位置吗?我找不到它 - 每个示例都在没有任何参数的情况下构建了这个对象。
  • 我已经按照jtreminio.com/2013/03/… 的教程进行操作。您可以在教程开始时立即看到它。
  • 抱歉,您指的是什么文档?它在 AuthorizeNetAim 文档上吗?如果是我还没有看到文档。我只是按照本教程进行操作。我想我必须查找图书馆的 API 本身。
  • 我参考了 github 上的文档。也许您指出的教程是基于该库的旧版本,或者只是有一个错误。看看这个项目的单元测试。请注意 AuthorizeNetRequest 类将 API_ID 和 TRANSACTION_KEY 作为其参数。
猜你喜欢
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2015-11-13
  • 2018-04-15
  • 2016-07-22
  • 1970-01-01
  • 2015-11-08
相关资源
最近更新 更多