jjzjj

php - 扩展 Laravel 的抽象 TestCase 类时出错(错误是说我的扩展类必须是抽象的)

coder 2024-04-11 原文

我希望有人能帮助我。我正在使用 laravel 4 并且我正在编写我的第一个单元测试一段时间但遇到了麻烦。我正在尝试扩展 TestCase 类,但出现以下错误:

PHP Fatal error:  Class registrationTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Illuminate\Foundation\Testing\TestCase::createApplication) in /home/john/www/projects/MyPainChart.com/app/tests/registrationTest.php on line 4

现在,如果我做对了,那么错误是指一个方法是抽象的,那么它所在的类也必须是抽象的。正如您从下面的 TestCase 类中看到的那样,它是抽象的。我已经搜索了这个错误,但是却画了一个空白。

试图在 Laracasts 上关注这个 Actor https://laracasts.com/lessons/tdd-by-example尽管您必须是订阅者才能观看视频,但文件位于视频下方,正如您所见,我所做的与 Jeffrey Way 没有什么不同。

我的测试:

<?php

class registrationTests extends \Illuminate\Foundation\Testing\TestCase
{

    /**
     * Make sure the registration page loads
     * @test
     */
    public function make_sure_the_registration_page_loads_ok()
    {
        $this->assertTrue(true);
    }
}

TestCase 类的开头:

<?php namespace Illuminate\Foundation\Testing;

use Illuminate\View\View;
use Illuminate\Auth\UserInterface;

abstract class TestCase extends \PHPUnit_Framework_TestCase {

顺便说一句——默认情况下,Laravel 测试类不会自动加载,所以我尝试了完全限定类名和使用 Illuminate\Foundation\Testing,然后只是扩展 TestCase。我知道它可以将其视为当我没有完全限定名称时它会提示找不到该类。我也试过:

composer dump-autoload

composer update

感谢任何帮助

最佳答案

根据你的错误信息:Class registrationTest contains 1 abstract method Base Class contains an abstract method 并且当 Child Class 使用抽象方法扩展另一个类,然后子类应实现 Base class 中可用的抽象方法。所以,registrationTest 是子类,\Illuminate\Foundation\Testing\TestCase 是基类,它包含一个抽象方法:

\Illuminate\Foundation\Testing\TestCase中的一个抽象方法:

abstract public function createApplication();

因此,在您的子类/registrationTest 中,您必须实现此方法:

public function createApplication(){
    //...
}

但是,实际上您不需要直接扩展 \Illuminate\Foundation\Testing\TestCase 因为在 app/tests 文件夹中有一个类 TestCase/TestCase.php 并且您可以改为扩展此类:

// In app/tests folder
class registrationTest extends TestCase {
    //...
}

TestCase.php 类如下所示:

class TestCase extends Illuminate\Foundation\Testing\TestCase {

public function createApplication()
{
        $unitTesting = true;
        $testEnvironment = 'testing';
        return require __DIR__.'/../../bootstrap/start.php';
    }
}

请注意,TestCase 已实现抽象方法 createApplication,因此您无需在类中扩展它。您应该使用 TestCase 类作为基类来创建测试用例。因此,在 app/tests 文件夹中创建您的测试并创建如下类:

class registrationTest extends TestCase {
    public function testBasicExample()
    {
        $crawler = $this->client->request('GET', '/');
        $this->assertTrue($this->client->getResponse()->isOk());
    }
}

阅读Class Abstraction .

关于php - 扩展 Laravel 的抽象 TestCase 类时出错(错误是说我的扩展类必须是抽象的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23450867/

有关php - 扩展 Laravel 的抽象 TestCase 类时出错(错误是说我的扩展类必须是抽象的)的更多相关文章

  1. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  2. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  3. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  4. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

    我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

  5. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  6. c - mkmf 在编译 C 扩展时忽略子文件夹中的文件 - 2

    我想这样组织C源代码:+/||___+ext||||___+native_extension||||___+lib||||||___(Sourcefilesarekeptinhere-maycontainsub-folders)||||___native_extension.c||___native_extension.h||___extconf.rb||___+lib||||___(Rubysourcecode)||___Rakefile我无法使此设置与mkmf一起正常工作。native_extension/lib中的文件(包含在native_extension.c中)将被完全忽略。

  7. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  8. ruby - 安装libv8(3.11.8.13)出错,Bundler无法继续 - 2

    运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin

  9. ruby-on-rails - 测试我的 Ruby gem:Shoulda::Matchers:Module (NoMethodError) 的未定义方法 `configure' - 2

    我正在开发我的第一个Rubygem,并捆绑了cucumber、rspec和shoulda-matches进行测试。当我运行rspec时,出现以下错误:/app/my_gem/spec/spec_helper.rb:6:in`':undefinedmethod`configure'forShoulda::Matchers:Module(NoMethodError)这是我的gem规范:#my_gem.gemspec...Gem::Specification.newdo|spec|......spec.add_development_dependency"activemodel"spec.a

  10. ruby-on-rails - Heroku 吃掉了我的自定义 HTTP header - 2

    我正在使用Heroku(heroku.com)来部署我的Rails应用程序,并且正在构建一个iPhone客户端来与之交互。我的目的是将手机的唯一设备标识符作为HTTPheader传递给应用程序以进行身份​​验证。当我在本地测试时,我的header通过得很好,但在Heroku上它似乎去掉了我的自定义header。我用ruby​​脚本验证:url=URI.parse('http://#{myapp}.heroku.com/')#url=URI.parse('http://localhost:3000/')req=Net::HTTP::Post.new(url.path)#boguspara

随机推荐