我正在为Laravel 5.1控制器编写单元测试,但在尝试测试搜索方法时遇到问题:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeControllerTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testGetIndex(){
$response = $this->call('GET', '/');
$this->assertEquals(200, $response->getStatusCode());
$this->assertViewHas(['menu_highlight',
'page_title',
'meta_description',
'featured_reviews',
'featured_categories',
'images',
'reviews']);
}
public function testGetSearch(){
$response = $this->call('GET', '/search?search_terms=test');
$this->assertEquals(200, $response->getStatusCode());
/* problem arises bellow: */
$this->assertViewHas(['page_title',
'meta_description',
'products',
'reviews',
'brands',
'images',
'categories',
'review_reporting_reasons',
'search_terms']);
}
}
testGetIndex()方法是O.K.问题出现在testGetSearch()方法中。什么可能导致错误?
HomeController代码如下:
<?php namespace App\Http\Controllers;
use View;
use Input;
use Settings;
use DB;
use App\Brand;
use App\Category;
use App\Review;
use App\Product;
use App\Image;
use App\User;
use App\FeaturedReview;
use App\ReviewReportingReason;
class HomeController extends Controller
{
public function getIndex() {
$featured_reviews = FeaturedReview::OrderBy('id', 'DESC')->take(5)->get();
$reviews = Review::where('active', '=', 1)->OrderBy('id', 'DESC')->take(5)->get();
$featured_categories = Category::OrderBy('id', 'DESC')->where('featured', '=', 1)->get();
$images = Image::OrderBy('id', 'DESC')->take(5)->get();
if ($reviews) {
return view('home')->with('menu_highlight', 'menu_home')
->with('page_title', Settings::get('site_name'))
->with('meta_description', Settings::get('meta_description'))
->with('featured_reviews', $featured_reviews)
->with('featured_categories', $featured_categories)
->with('images', $images)
->with('reviews', $reviews);
}
else {
throw new \Exception('Please install the application');
}
}
public function getSearch() {
$search_terms = Input::get('search_terms');
try {
$products = Product::LeftJoin('categories', 'categories.id', '=', 'products.category_id')
->LeftJoin('brands', 'brands.id', '=', 'products.brand_id')
->select(
'products.*',
'categories.id as cid',
'categories.name as cname',
'brands.id as bid',
'brands.name as bname'
)
->whereRaw("MATCH (products.name) AGAINST (?)", array($search_terms))
->orWhereRaw("MATCH (categories.name) AGAINST (?)", array($search_terms))
->orWhereRaw("MATCH (brands.name) AGAINST (?)", array($search_terms))
->where('products.active', '=', 1)
->OrderBy('products.rating', 'DESC')
->OrderBy('products.reviews_count', 'DESC')
->GroupBy('products.id')
->paginate(Settings::get('front_end_pagination'));
$products->setPath('search');
}
catch(\Exception $e) {
return view('/products/no_products')->with('search_terms', $search_terms);
}
$reviews = Review::whereRaw("MATCH (text) AGAINST (?)", array($search_terms))
->OrWhereRaw("MATCH (title) AGAINST (?)", array($search_terms))
->OrWhereRaw("MATCH (pros) AGAINST (?)", array($search_terms))
->OrWhereRaw("MATCH (cons) AGAINST (?)", array($search_terms))
->where('active', '=', 1)
->orWhere('title', 'LIKE', '%' . $search_terms . '%')
->orWhere('pros', 'LIKE', '%' . $search_terms . '%')
->orWhere('cons', 'LIKE', '%' . $search_terms . '%')
->OrderBy('id', 'DESC')
->paginate(Settings::get('front_end_pagination'));
$reviews->setPath('search');
$brands = DB::table('brands')
->select(DB::raw('brands.*, count(reviews.id) as reviews, (select count(products.id) from products where products.brand_id = brands.id) as products'))
->LeftJoin('products', 'brands.id', '=', 'products.brand_id')
->LeftJoin('reviews', 'reviews.product_id', '=', 'products.id')
->WhereRaw("MATCH (brands.name) AGAINST (?)", array($search_terms))
->GroupBy('brands.id')
->OrderBy('reviews', 'DESC')
->paginate(Settings::get('front_end_pagination'));
$brands->setPath('search');
$categories = DB::table('categories')
->select(DB::raw('categories.*, count(reviews.id) as reviews, (select count(products.id) from products where products.category_id = categories.id) as products'))
->LeftJoin('products', 'categories.id', '=', 'products.category_id')
->LeftJoin('reviews', 'reviews.product_id', '=', 'products.id')
->WhereRaw("MATCH (categories.name) AGAINST (?)", array($search_terms))
->GroupBy('categories.id')
->OrderBy('reviews', 'DESC')
->paginate(Settings::get('front_end_pagination'));
$categories->setPath('search');
$images = Image::whereRaw("MATCH (description) AGAINST (?)", array($search_terms))
->OrderBy('id', 'DESC')
->paginate(Settings::get('front_end_pagination'));
$review_reporting_reasons = ReviewReportingReason::OrderBy('ID', 'DESC')->get();
return view('home/search_results')
->with('page_title', 'Search results for' . ' ' . $search_terms)
->with('meta_description', 'Search results for '.$search_terms)
->with('products', $products)
->with('reviews', $reviews)
->with('brands', $brands)
->with('images', $images)
->with('categories', $categories)
->with('review_reporting_reasons', $review_reporting_reasons)
->with('search_terms', $search_terms);
}
}
答案 0 :(得分:2)
问题发生在视图文件中,其中带有模式窗口的html源代码的文件包含在@include(&#39; modal&#39;)中而不是@yield(&#39; modal&#39; )。该文件没有格式化为刀片模板,只是没有刀片指令的普通html。