0

Hi I would like to test very simple method in Laravel 4.1. I am starting with tests in laravel and I would appreciate a little help here.

I am using phpspec..

Here is method:

public function hasTag($codename)
{
    $tag = $this->tags()->where('codename', '=', $codename)->first();
    if (is_null($tag))
    {
        return false;
    }

    return true;
}
Dusan Plavak
  • 4,457
  • 4
  • 24
  • 35

1 Answers1

0

You could do something like:

function it_looks_for_tag_correctly(Tag $tag)
{
    $tag->setCodeName('Codename');  
    $this->addTag($tag);  // I guess you have a method to add tags

    $this->hasTag($tag)->shouldReturn(true); 
}
gvf
  • 1,039
  • 7
  • 6
  • 1
    As I find out on internet, probably the best way how to do it is use design patter Repositories, so I can then mock the repository instead the Eloquent... – Dusan Plavak Jul 04 '15 at 15:55
  • @DusanPlavak that's exactly the way to go. – Jakub Zalas Jul 06 '15 at 08:46
  • I don't see any repositories here. You seemm to have an entity that has a collection of tags, and you look for a specific tag in that collection. – gvf Jul 06 '15 at 09:04
  • @gvf exactly there is none. But if you are writing unit test, then you do not want to touch DB. Because there is no Repository answer should be to create one – Dusan Plavak Jul 06 '15 at 09:25
  • You are not testing any persistence here, just the existence of an item in a collection. I assume $this->tags() is a collection of tags attached to the entity, right? – gvf Jul 06 '15 at 09:34