Wednesday, November 26, 2008

Re: CakePHP Unit Test Model Woes :(

Mark:
Yeah, but the fixture file I've posted was created using the
'fixturize' script from the guys at debuggable:
http://debuggable.com/posts/fixturize-shell---generate-your-fixtures-automatically:48c12ad0-2350-4b5a-94d4-5f424834cda3

It reads from an existing table (in the default db) and also reads
records in that table to build a fixture which contains $records but,
as you've pointed out, does not include an import statement for a
table or model. I think this is probably intentional but I haven't
been able to get the Debuggable example working.

This is what I mean about everyone seems to be building their unit
tests slightly differently.
Andy

On Nov 26, 5:31 pm, mark_story <mark.st...@gmail.com> wrote:
> Your fixture doesn't define a var $fields nor does it use import.
> Therefore the fixture has no way of creating a table schema.  You need
> to define fields information or import a table definition.  Seehttp://book.cakephp.org/view/360/Creating-fixtures
>
> -Mark
>
> On Nov 26, 10:43 am, pragna <a...@andyjordan.com> wrote:
>
> > mark_story: thanks - i've changed fixture to 'app.user' & removed
> > $this->loadFixtures.
> > valikeys: thanks - i've removed the redundant App::import.
>
> > Test now is:
> > <?php
> > class UserTestCase extends CakeTestCase {
> >         var $fixtures = array( 'app.user' );
>
> >         function setUp() {
> >     $this->User = ClassRegistry::init('User');
> >         }
>
> >         function testEnabledUsers() {
> >                 $result = $this->User->enabledUsers(array('id', 'email'));
> >                 $expected = array(
> >                         array('UserTest' => array( 'id' => 1, 'email' => 'a...@xyz.com' )),
> >                         array('UserTest' => array( 'id' => 2, 'email' => '...@joe.com' ))
> >                 );
> >                 $this->assertEqual($result, $expected, 'Custom error message goes
> > here. Original: %s');
> >         }}
>
> > ?>
>
> > However still produces the same errors, the 1st error being:
> > Unexpected PHP error [Undefined property: UserFixture::$fields]
> > severity [E_NOTICE] in [C:\xampp\htdocs\cake\cake\tests\lib
> > \cake_test_fixture.php line 159.
> > This is in the function drop() which the comments say is "Run after
> > all tests executed, should return SQL statement to drop table for this
> > fixture".
> > he line is: $this->Schema->_build(array($this->table => $this->fields));
>
> > and using debug $this->table is 'users' and this->fields is NULL.
>
> > I'm new to CakePHP and pretty new to PHP as well, so any help working
> > out what's goin on is appreciated.
>
> > Thanks,
> > Andy
>
> > However I'm still finding errors!
>
> > <pre name="code" class="Cpp">
> > ….My code here…
> > </pre>
>
> > On Nov 26, 2:42 pm, validkeys <kyle.da...@indieworldonline.com> wrote:
>
> > > RE: 3) I don't think you need to App::import your model if you use
> > > ClassRegistry::init.  Not 100% on that, and it shouldn't hurt even if
> > > you do.
>
> > > You don't, ClassRegistry::init handles the App::import
>
> > > On Nov 26, 9:15 am, mark_story <mark.st...@gmail.com> wrote:
>
> > > > Your fixture name needs to be 'app.user'.  Also you only need to
> > > > manually load fixtures is you have var $autoFixtures set to false.
>
> > > > -Mark
>
> > > > On Nov 25, 5:04 pm, MattC <m...@mcurry.net> wrote:
>
> > > > > This may or may not fix your problem but:
> > > > > 1) You test db should be empty.  The framework will automatically
> > > > > create/drop the tables for each test.
> > > > > 2) You don't need to call "loadFixtures" at the start of each test.
> > > > > 3) I don't think you need to App::import your model if you use
> > > > > ClassRegistry::init.  Not 100% on that, and it shouldn't hurt even if
> > > > > you do.
> > > > > 4) In my fixtures I have the line:
> > > > > var $import = array('table' => 'name', 'records' => false);
> > > > > I had to use the actually table name to get everything working with
> > > > > ClassRegistry setting the right database.  It didn't work if I just
> > > > > specified the model.
>
> > > > > Hope that helps.
> > > > > -Matthttp://www.pseudocoder.com
>
> > > > > On Nov 25, 4:29 pm, pragna <a...@andyjordan.com> wrote:
>
> > > > > > I've been struggling for a couple of days now to build a very simple
> > > > > > 'model' unit test.
>
> > > > > > CakePHPs "Convention over Configuration" should help me out here and
> > > > > > so should the Cookbook.
>
> > > > > > With Unit Tests though, it's quite hard to uncover the what the
> > > > > > conventions are. The filename conventions described in the Cookbook
> > > > > > (1.2) differ from, for example, the fixture files created using Bake.
> > > > > > Also the guys at Debuggable have re-factored and simplified the
> > > > > > building of model tests as well as sharing a fixturize script that
> > > > > > automates the building of fixtures.
>
> > > > > > All great stuff!
> > > > > > If only I could get the simplest test up and running :(
>
> > > > > > My failing code is here:http://oneyeareatingcake.blogspot.com/2008/11/cakephp-unit-test-model...
>
> > > > > > It is the most basic of Unit Tests on a simple findAll function in the
> > > > > > most basic of models. The reason this is not working has to be that
> > > > > > I'm breaking the convention somewhere. Maybe in file naming or in the
> > > > > > way I'm including the fixture?
>
> > > > > > I'd be very grateful if someone could get me out of this mess by
> > > > > > pointing out what I've got wrong.
> > > > > > Thanks,
> > > > > > Andy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: