2017-06-02 76 views
0

我對測試非常陌生,但現在發現自動化我的測試很重要。Laravel testing失敗斷言兩個字符串相等

我有一個測試工作正常,直到它到達鏈接'/購物車'它的鏈接'/購物車'沒有問題,但任何其他的鏈接,我嘗試點擊事後總是結束回來在大車。

這是我嘗試離開購物車後導致的錯誤。

Failed asserting that two strings are equal. 
--- Expected 
+++ Actual 
@@ @@ 
-'http://ngwenya-mtb.dev/events' 
+'http://ngwenya-mtb.dev/cart' 

這裏是我的測試腳本

use Illuminate\Foundation\Testing\WithoutMiddleware; 
use Illuminate\Foundation\Testing\DatabaseMigrations; 
use Illuminate\Foundation\Testing\DatabaseTransactions; 

class ExampleTest extends TestCase { 




//use Illuminate\Foundation\Testing\WithoutMiddleware; 
//use DatabaseTransactions; 
//use withoutMiddleware; 
//use DatabaseMigrations; 
/** 
* 
* A basic functional test example. 
* Please choose a unique email address for your new participant 
* @return void 
*/ 
public function testNewUserRegistration() { 

    $this->visit('http://ngwenya-mtb.dev/') 
     // View Event 
     ->click('View event details') 
     ->seePageIs('/event?id=30') 

     ->click('#enter-race47') 
     ->press('Enter yourself to this race') 

     ->seePageIs('/events/courses/register/addtocart') 
     //->withSession(['This email is already registered' => 'alert-danger']) 

     ///////////////////////////////////////////// 
     // Fill the register for for new user 
     ///////////////////////////////////////////// 
     ->type('Bingo', 'first_name') 
     ->type('11111111', 'password') 
     ->type('11111111', 'password_confirmation') 
     ->type(''.substr(md5(time()), 0, 12).'@tesing.com', 'email') 
     //->check('terms') 
     ->select('Male', 'gender') 
     ->select('1985', 'year') 
     ->select('07', 'month') 
     ->select('21', 'day') 
     ->select('Small', 'shirt_size') 
     ->select('Swaziland ID', 'id_type') 
     ->type('badassnumber', 'id_number') 
     ->select('Swazi', 'nationality') 
     //Contact details Physical 
     ->type('Dawlish', 'town_physical') 
     ->select('Swaziland', 'country_physical') 
     ->type('864741', 'phone_cell') 
     //Emergency contact details 1 
     ->type('Simon', 'emergency_contact_1') 
     ->type('Brother', 'emergency_relationship_1') 
     ->type('864741', 'emergency_phone_1'); 


     $this->press('Register'); 
     $this->seePageIs('/cart'); 


     ///////////////////////////////////////////// 
     // Add a new user 
     ///////////////////////////////////////////// 

     $this->visit('http://ngwenya-mtb.dev/'); 
     $this->click('#events-link') 

     ->seePageIs('/events'); 
     dd($this->response->getContent());exit; 
     $this->click('#event-30'); 
     $this->seePageIs('/event?id=30'); 

     $this->click('#enter-race48'); 
     $this->press('Enter someone else to this race'); 
     $this->seePageIs('/events/courses/register/addtocart');    
    } 

} 

一切正常,直到此評論

///////////////////////////////////////////// 
// Add a new user 
///////////////////////////////////////////// 

這裏是我的登記控制器

<?php 

namespace App\Http\Controllers; 

use Vinkla\Hashids\HashidsManager; 
use Illuminate\Routing\Controller as BaseController; 
use Sentinel\FormRequests\RegisterRequest; 
use Sentinel\FormRequests\EmailRequest; 
use Sentinel\FormRequests\ResetPasswordRequest; 
use Sentinel\Repositories\Group\SentinelGroupRepositoryInterface; 
use Sentinel\Repositories\User\SentinelUserRepositoryInterface; 
use Sentinel\Traits\SentinelRedirectionTrait; 
use Sentinel\Traits\SentinelViewfinderTrait; 
use Sentry; 
use View; 
use Request; 
use Event; 
use Redirect; 
use Session; 
use Config; 
use App\Models\Users; 
use Illuminate\Support\Facades\Input; 
use Gloudemans\Shoppingcart\Facades\Cart; 

class RegistrationController extends BaseController 
{ 
    /** 
    * Traits 
    */ 
    use SentinelRedirectionTrait; 
    use SentinelViewfinderTrait; 

    /** 
    * Constructor 
    */ 
    public function __construct(
     SentinelUserRepositoryInterface $userRepository, 
     SentinelGroupRepositoryInterface $groupRepository, 
     HashidsManager $hashids 
    ) { 
     $this->userRepository  = $userRepository; 
     $this->groupRepository  = $groupRepository; 
     $this->hashids    = $hashids; 
    } 

    /** 
    * Show the registration form, if registration is allowed 
    * 
    * @return Response 
    */ 
    public function registration() 
    { 
     // Is this user already signed in? If so redirect to the post login route 
     if (Sentry::check()) { 
      return $this->redirectTo('session_store'); 
     } 

     //If registration is currently disabled, show a message and redirect home. 
     if (! config('sentinel.registration', false)) { 
      return $this->redirectTo(['route' => 'home'], ['error' => trans('Sentinel::users.inactive_reg')]); 
     } 

     // All clear - show the registration form. 
     return $this->viewFinder(config('sentinel.view.user_register', 'Sentinel::users.register')); 
    } 

    /** 
    * Process a registration request 
    * 
    * @return Response 
    */ 
    public function register(RegisterRequest $request) 
    { 

     // Gather input 
     $data = $request->all(); 

     // collect cart items 
     $email = Input::get('email'); 
     $course_id = Input::get('course_id'); 
     $event_name = Input::get('event_name'); 
     $entry_fee = Input::get('entry_fee'); 

     // check user exists 
     if (Users::where('email', '=', $email)->exists()) { 
      // user found 
      $request->session()->flash('alert-danger', 'Warning: This email is already registered.'); 
      Input::flash(); 
      return View::make('sentinel.users.register') 
        ->with('course_id',$course_id) 
        ->with('event_name',$event_name) 
        ->with('entry_fee',$entry_fee); 
     } 

     // Add user and course to cart 
     if ($course_id) { 
      $firstUserRowId = Cart::add($course_id, $event_name , 1, $entry_fee, [ 
        'first_name' => Input::get('first_name'), 
        'last_name' => Input::get('last_name'), 
        'email' => Input::get('email'), 
        'no_email' => 0, 
        'master_user' => 1, 
        'gender' => Input::get('gender'), 
        'dob' => Input::get('dob'), 
        'shirt_size' => Input::get('shirt_size'), 
        'id_type' => Input::get('id_type'), 
        'id_number' => Input::get('id_number'), 
        'nationality' => Input::get('nationality'), 
        'phone_cell' => Input::get('phone_cell'), 
        'town_physical' => Input::get('town_physical'), 
        'country_physical' => Input::get('country_physical'), 
        'emergency_contact_1' => Input::get('emergency_contact_1'), 
        'emergency_relationship_1' => Input::get('emergency_relationship_1'), 
        'emergency_phone_1' => Input::get('emergency_phone_1'), 
       ]);    
     } 



     // get email from request 
     $email = $request->only('email'); 
     foreach ($email as $userModel) {} 

     // Edit date of birth from request 
     $year = Input::get('year'); 
     $month = Input::get('month'); 
     $day = Input::get('day');  
     $dob = $year.'-'.$month.'-'.$day; 
     $data['dob'] = $dob; 

     // Attempt Registration 
     $result = $this->userRepository->store($data);       

     // Log user in 
     FunctionsController::loginUser($userModel); 

     // It worked! Use config to determine where we should go. 
     return $this->redirectViaResponse('registration_complete', $result); 
    } 

    /** 
    * Activate a new user 
    * 
    * @param int $id 
    * @param string $code 
    * 
    * @return Response 
    */ 
    public function activate($hash, $code) 
    { 
     // Decode the hashid 
     $id = $this->hashids->decode($hash)[0]; 

     // Attempt the activation 
     $result = $this->userRepository->activate($id, $code); 

     // It worked! Use config to determine where we should go. 
     return $this->redirectViaResponse('registration_activated', $result); 
    } 

    /** 
    * Show the 'Resend Activation' form 
    * 
    * @return View 
    */ 
    public function resendActivationForm() 
    { 
     return $this->viewFinder('Sentinel::users.resend'); 
    } 

    /** 
    * Process resend activation request 
    * @return Response 
    */ 
    public function resendActivation(EmailRequest $request) 
    { 
     // Resend the activation email 
     $result = $this->userRepository->resend(['email' => e($request->get('email'))]); 

     // It worked! Use config to determine where we should go. 
     return $this->redirectViaResponse('registration_resend', $result); 
    } 

    /** 
    * Display the "Forgot Password" form 
    * 
    * @return \Illuminate\View\View 
    */ 
    public function forgotPasswordForm() 
    { 
     return $this->viewFinder('Sentinel::users.forgot'); 
    } 


    /** 
    * Process Forgot Password request 
    * @return Response 
    */ 
    public function sendResetPasswordEmail(EmailRequest $request) 
    { 
     // Send Password Reset Email 
     $result = $this->userRepository->triggerPasswordReset(e($request->get('email'))); 

     // It worked! Use config to determine where we should go. 
     return $this->redirectViaResponse('registration_reset_triggered', $result); 
    } 

    /** 
    * A user is attempting to reset their password 
    * 
    * @param $id 
    * @param $code 
    * 
    * @return Redirect|View 
    */ 
    public function passwordResetForm($hash, $code) 
    { 
     // Decode the hashid 
     $id = $this->hashids->decode($hash)[0]; 

     // Validate Reset Code 
     $result = $this->userRepository->validateResetCode($id, $code); 

     if (! $result->isSuccessful()) { 
      return $this->redirectViaResponse('registration_reset_invalid', $result); 
     } 

     return $this->viewFinder('Sentinel::users.reset', [ 
      'hash' => $hash, 
      'code' => $code 
     ]); 
    } 

    /** 
    * Process a password reset form submission 
    * 
    * @param $hash 
    * @param $code 
    * @return Response 
    */ 
    public function resetPassword(ResetPasswordRequest $request, $hash, $code) 
    { 
     // Decode the hashid 
     $id = $this->hashids->decode($hash)[0]; 

     // Gather input data 
     $data = $request->only('password', 'password_confirmation'); 

     // Change the user's password 
     $result = $this->userRepository->resetPassword($id, $code, e($data['password'])); 

     // It worked! Use config to determine where we should go. 
     return $this->redirectViaResponse('registration_reset_complete', $result); 
    } 
} 
+0

請向我們展示處理註冊表單提交的控制器方法。 – Jamesking56

+0

謝謝。我已經添加了控制器。這個函數是'公共函數寄存器(RegisterRequest $ request)' – warmwhisky

+0

你可以顯示'$ this-> redirectViaResponse'方法嗎? – Jamesking56

回答

0

看來,當你點擊和你的鏈接「註冊」你的重定向失敗,所以檢查你是否有多個「註冊」鏈接/按鈕,如果他們指向正確的URL

爲了簡單的調試,你應該減少每個測試的斷言,你將獲得知名度:)

+0

嘿,謝謝你的迴應。註冊似乎工作,因爲數據庫與用戶的詳細信息更新,然後重定向到購物車。這是重定向到購物車的問題嗎? – warmwhisky

+0

填寫表格後,如果您手動點擊註冊表,您的表格是否會提交到/購物車頁面?或者你有一箇中間頁面? –

+0

不,沒有中間頁面。它進行登記,然後路由到購物車。 – warmwhisky