简体   繁体   English

Laravel 5.1测试:收到“响应不是视图”错误

[英]Laravel 5.1 testing: Getting “The response was not a view” error

I'm writting a unit test for a Laravel 5.1 controller, but having problem while trying to test search method: 我正在为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']);

  }
}

The testGetIndex() method is OK the problem arises with the testGetSearch() method. testGetIndex()方法可以,testGetSearch()方法会出现问题。 What might cause the error? 是什么导致错误?

HomeController code is as follows: 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);
    }
}

The problem was in the view file, where a file with a html source code for a modal window was included with @include('modal') instead of @yield('modal'). 问题出在视图文件中,其中包含模态窗口的html源代码的文件包含在@include('modal')中,而不是@yield('modal')中。 The file was not formated as a blade template, just plain html without blade directives. 该文件未格式化为刀片模板,而只是没有刀片指令的纯HTML格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM