我已经使用Laravel一段时间了,我已经阅读了很多关于依赖注入的可测试代码.在谈到Facades和Mocked Objects时,我已经陷入了困惑.我看到两种模式:
class Post extends Eloquent {
protected $guarded = array();
public static $rules = array();
}
Run Code Online (Sandbox Code Playgroud)
这是我的帖子模型.我可以跑来Post::all();从我的博客上获取所有帖子.现在我想将它合并到我的控制器中.
我的第一直觉是将Post模型作为依赖注入:
class HomeController extends BaseController {
public function __construct(Post $post)
{
$this->post = $post;
}
public function index()
{
$posts = $this->posts->all();
return View::make( 'posts' , compact( $posts );
}
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试看起来像这样:
<?php
use \Mockery;
class HomeControllerTest extends TestCase {
public function tearDown()
{
Mockery::close();
parent::tearDown();
}
public function testIndex()
{
$post_collection = new StdClass();
$post = …Run Code Online (Sandbox Code Playgroud) 你好.我得到了一个iPhone应用程序,作为其功能的一部分,在请求正文中向服务器发送带有二进制jpeg图像的请求.我必须找到一种方法来读取二进制数据,通过socket.io将其发送到客户端,并将映像的副本记录到服务器上的磁盘.
我尝试了一些没有运气的方法.首先我尝试了这个:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
console.log("Request In");
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
fs.writeFile("./1.jpeg", body, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
});
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Image Saved!');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
Run Code Online (Sandbox Code Playgroud)
这没用.我无法获得可在Windows Photo Viewer中打开的图像.Windows照片查看器给了我错误Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or …