phpspec和laravel设置 [英] phpspec and laravel setup
问题描述
我正在建立一个新的Laravel项目并集成PHPSpec.我很难找到可以与Laravel一起正常工作的phpspec.yml文件的良好工作示例.与Rails中的RSpec相似.
I am setting up a new Laravel project and integrating PHPSpec. I am having trouble finding a good working example of the phpspec.yml file that would work neatly with Laravel. In a similar way to RSpec in Rails.
我想要的文件夹结构如下
My desired folder structure would be as follows
spec/
models/
controllers/
app/
models/
controllers/
我的phpspec.yml当前看起来像这样:
My phpspec.yml currently looks like this:
suites:
controller_suite:
namespace: Controller
spec_path: 'spec/controllers'
src_path: 'app/controllers'
model_suite:
namespace: Model
spec_path: 'spec/models'
src_path: 'app/models'
我将与模型相关的测试复制到spec/models文件夹中,但是当我运行phpspec"时,它不运行任何测试.我还意识到,在Laravel中,名称空间不是'Model'和'Controller',也许更像Eloquent和BaseController.
I copied my Model related tests in the spec/models folder, but when I 'phpspec run' it does not run any of them. I also realise that the namespace is not 'Model' and 'Controller' in Laravel, perhaps more like Eloquent and BaseController..
我也在使用此扩展名: https://github.com/BenConstable/phpspec-laravel
I am also using this extension: https://github.com/BenConstable/phpspec-laravel
我不确定该如何设置,也找不到任何可行的示例.预先感谢!
I'm not sure how to set this up and cannot find any working examples. Thanks in advance!
Jeffrey Way在另一个论坛上提供的建议:
Advice founbd on another forum from Jeffrey Way:
您可以使用Behat测试您的控制器.PHPSpec不能替代为此(或代码接受).
You can test your controllers with Behat. PHPSpec isn't a substitute for it (or Codeception).
更新:
此后,我决定改用Codeception,因为它似乎已经很好地集成到Laravel中并被广泛使用.
I've since decided to use Codeception instead as it seems to integrate neatly into Laravel and widely used.
推荐答案
从 Ben Constable 通过电子邮件亲切回答:
Kindly answered via email from Ben Constable :
使用Laravel的默认文件布局来设置PHPSpec非常困难,我还没有弄清楚该怎么做.但是,您可以使用略有不同的布局来使它工作,例如:
Setting up PHPSpec with Laravel with its default file layout is pretty difficult, and I haven’t yet figured out how to do it. However, you can get it working with a slightly different layout, like:
- app
- Controllers
- MyController.php
- Models
- MyModel.php
- spec
- Controllers
- MyControllerSpec.php
- Models
- MyModelSpec.php
然后,在您的 phpspec.yml
中,您将拥有:
then, in your phpspec.yml
you’d have:
extensions:
- PhpSpec\Laravel\Extension\LaravelExtension
suites:
laravel_controller_suite:
namespace: Controllers
src_path: app
laravel_model_suite:
namespace: Models
src_path: app
laravel_extension:
testing_environment: 'testing'
最后,您需要修改 composer.json
以在自动加载类映射中包含 app/
.您的模型,控制器以及随后将被命名为名称的任何东西,例如:
and finally, you’d need to modify your composer.json
to include app/
in the autoload class map. Your models, controllers and whatever would then be namespaced, like:
<?php namespace Controllers;
use Controller;
class MyController extends Controller {}
那应该可以解决您的问题.顺便说一句,当我制作Laravel项目时,我一直将所有内容放在 app/src/MyVendor/MyNamespace/Controllers
等中,我更喜欢将其作为布局(使源远离配置文件和其他文件,类似于Laravel软件包的布局.
That should sort you out. Just as an aside, when I’ve been making Laravel projects I’ve been putting everything in app/src/MyVendor/MyNamespace/Controllers
etc, which I prefer as a layout (keeps the source away from the config and other files, and is similar to the layout of Laravel Packages).
将来,我将尝试研究它,看看是否可以使PHPSpec与默认的Laravel布局一起使用-如果可以,我将在GitHub上更新该项目.
In the future, I will try and look into it and see if I can get PHPSpec working with the default Laravel layout - I’ll update the project on GitHub if/when I do.
这篇关于phpspec和laravel设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!