开发者

Starting with BDD (using Behat and Symfony2 as background)

开发者 https://www.devze.com 2023-04-09 17:27 出处:网络
I\'m just starting with BDD and this instructions about Behat but I\'m missing an example a bit more complex, Product-Category example is too simple (but

I'm just starting with BDD and this instructions about Behat but I'm missing an example a bit more complex, Product-Category example is too simple (but necessary at first of course..) when you want to go beyond..

I'm working with models that doesn't have a unique field in order to do this:

$product = this->getRepository('AcmeDemoBundle:Product')->findOneByName($productName);

In my case I have a relation 1:1:

Room
  hotel_id
  ...

Default configuration
  room_id
  name    //"single room", "double room"...
  price
  ...

So, when I want to create the scenario "Scenario: A room has a default configuration" I'd like to start this way:

I have a room XXX

but I can't because I don't have any field like "name" or any other that is unique, so I just write:

I have a room

The problem comes when I want to retrieve the room to add a default configuration like in the example Product-Category ($product = $this->getRepository('AcmeDemoBundle:Product')->findOneByName($productName);), I don't know what to do.., how to retrieve the room object I'm using to add a default configuration? or how to retrieve the default configuration object?

So, any idea how should I act?

EDIT:

After the response of everzet I want to add the scenario I'm interested in implement:

When I add a default configuration to a room
Then I should find a room has a default configuration

Maybe this scenario sounds strange, but as I said above, I don't have a unique field in neither in Room nor in Default Configuration.

So, what should 开发者_开发技巧be the functions in my .feature file?


All your scenario step definitions run inside single context class instance and every scenario has it's own context instance. It means, that you can set ivars on the current context in one step definition and read it's value in next step definition. It's even described in the Behat documentation ;-)

In your case, your I have a room step could save last persisted record id into context ivar and next step could use it's value to find a specific (last added) room in the database. Like that:

// Given I have a room
// …
$room = new Room();
$em->persist($room);
$em->flush();
$this->lastRoomId = $room->getId();
// …

// Then this room should have ...
// …
$room = $em->getRepository('AcmeDemoBundle:Room')
    ->findOneById($this->lastRoomId);
// …
0

精彩评论

暂无评论...
验证码 换一张
取 消