roelof's avatar

codeception. Why does a test succeed ?

Hello,

I installed codeception according to to website. composer update and bootstrap it.

Then I wrote this test :

<?php
class PlanCest
{
    public function validatePlan(UnitTester $t)
    {
        $plan = $t->createPlan();
        $plan->soort = null;
        $t->assertTrue($plan->validate(['soort'])) ; 


    }
}
?>

then I ran the test and both are succesfull but that is not good. in my validation I have put that soort is required so i cannot be empty or null.

Roelof

0 likes
11 replies
roelof's avatar

which code do you want ? This is the code I wrote for the test

RobinMalfait's avatar

The actual implementation the class with this method createPlan() :)

roelof's avatar

That is no method createplan There is a method create in the plan controller.

That one looks like this :

/**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('plan.create'); 
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {

         $validation = Validator::make(Input::all(), Plan::$rules); 

         if ($validation-> fails()) 
           {
           Return Redirect::back()->WithInput()->WithErrors($validation->messages()); 
         }

         $abonnementen = New Plan; 
         $abonnementen->soort = Input::get('soort'); 
         $abonnementen->bedrag  = Input::get('bedrag'); 
         $abonnementen->save();  

         Redirect::route('home.dashboard' ); 

    }
RobinMalfait's avatar

Hmm, btw code in English even when you are Dutch ;)

I've used Dutch in my code and it is not good, be consistent.

But, the code in the test is not that code or am I that wrong? :o

roelof's avatar

Sorry : I again do not understand what you mean with the last sentence.

RobinMalfait's avatar

I want to see this code createPlan()

(Ik wil de code zien van deze methode createPlan())

roelof's avatar

I said already that that method does not exist,

I try to check the validation of the model Plan, which is created and stored with the methods I already pasted here.

MThomas's avatar

The problem @malfait.robin is trying to point out that you call createPlan() on this line: $plan = $t->createPlan();. In order to call that method or function or whatever you want to name it you have to create that function and for us to help you we need to see that function.

You create a plan and perform a test on that plan, so you do you create that plan?

--- In Dutch ---

Het probleem dat @malfait.robin probeert duidelijk te maken is dat je de functie createPlan() op deze regel: $plan = $t->createPlan(); probeert uit te voeren. Maar hoe ziet die functie er uit? Het is geen standaard functie, dus je moet hem zelf hebben geschreven of gemaakt. Ofwel hoe ziet die functie er uit?

roelof's avatar

Hello,

I understand that . Like I said the function createPlan does not exist.

What I try to achieve is this :

I have a method create and store in my PlanController.

Within the store method the input is validated against some rules.

So I like to unit test the rules .

ozanhazer's avatar

(createPlan is a magic function provided by codeception so it should be creating an instance of the model class: Plan)

First of all you have a fundamental mistake: You're using Validator class in your controller to do the validation but you're testing the validate method of the model class.

Validator::make(Input::all(), Plan::$rules)

Either test the controller class for validation and not the model for this case, or implement the validate method inside the model class and use that in the controller too. Example validate method would be smt. like this:

public function validate($data)
{
    $v = Validator::make($data, self::$rules);
    return $v->passes();
}

Please or to participate in this conversation.