laravel 5.1中按功能返回url的单元测试

时间:2015-09-09 04:48:20

标签: laravel-5.1

这是我的路线

Route::group(['prefix' => 'frontend'], function() {
        Route::get('/cart', array('as' => 'cart.frontend', 'uses' => 'CartController@getCart'));
        Route::post('/cart/{rowId}/edit', array('as' => 'cart.edit', 'uses' => 'CartController@editCartItem'));
        }); 

这是我的CartController,其中getcart函数返回视图(' frontend / cart')和函数editCartItem返回url(' frontend / cart')

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Product;
use App\Sale;
use App\PurchasedProduct;
use App\ShippingAddress;
use App\BillingAddress;
use App\Payment;
use App\Member;
use App\Category;
use App\GiftCredit;
use App\CreditType;

class CartController extends Controller {
     public function getCart() 
                    {
                        $categories = Category::with('children')->get();

                        $purchasedProducts = \Cart::content();

                        $shippingAddress = null;
                        $billingAddress = null;

                        $creditTypes = CreditType::all();
                        $credits = GiftCredit::where('member_id', '1')->get();
                        //return $giftCredits;

                        if (\Session::has('shippingAddress'))
                        {
                            $shippingAddress = \Session::get('shippingAddress');
                        }

                        if (\Session::has('billingAddress'))
                        {
                            $billingAddress = \Session::get('billingAddress');
                        }

                        return view('frontend/cart', compact('purchasedProducts', 'shippingAddress', 'billingAddress', 'categories', 'credits', 'creditTypes'));
                    }

    public function editCartItem($rowId = null) 
        {
            //echo $rowId;
            //die();
            $qty = \Request::input('qty');

            \Cart::update($rowId, array('qty' => $qty));

            return redirect('frontend/cart');
        }
    }

这是我的观点('前端/购物车'),我在此处包含视图形式('前端/购物车')

    <form action="{{url('frontend/cart/'.$purchasedProduct->rowid.'/edit')}}" method="post" id="cartEditForm_{{$purchasedProduct->rowid}}">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="number" min="1" value="{{ $purchasedProduct->qty }}" name="qty" class="form-control qty" />
                     <div class="table-row btn-actions">
                        <button type="button" name="Edit" class="btn btn-sm editCart" data-rowid="{{$purchasedProduct->rowid}}">Edit</button>
                     </div>
  </form>

这是我的测试

 class ShopMen extends TestCase{
    public function testCartEdit(){
             $this->visit('/frontend/cart')
                  ->type('5', 'qty')
                  ->press('Edit')
                  ->seePageIs('/frontend/cart/277226b59367ffb14da52b69b86f8917/edit')
                  ->seePageIs('/frontend/cart');

        }
    }

1 个答案:

答案 0 :(得分:0)

发生此错误是因为filter方法找不到名为'qty'的输入。我可以看到你确实有一个'qty'字段,因此可能是页面中的HTML存在其他错误,导致过滤器无法找到该输入。