Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

#find_model does not use label to find the model

#25
create_model 'user: "Frank"'
create_model 'user: "Fred"'
find_model 'user: "Fred"' # => Finds user: "Frank" because he was created first.

I am assuming that is not the intended behaviour.

Reported by Bo Kirkeby · July 17th, 2010 @ 06:45 PM

State: new
Milestone: none
Assigned to: Ian White Ian White

Activity

  1. Ian White
    Ian White

    If you are using find_model to retrieve a model using the pickle ref, I think that the 'model' method is what you want...

    model 'user: "Fred"'

    or
    model! 'user: "Fred"' (raises if no such model is available in the scenario)

    find_model finds a model using the passed attrs, and stores it for pickle usage under the given named. So, in your case it finds the first model (because you;ve not given any attrs to find by) and stores it as 'user: "Fred"'

    Cheers,
    Ian

    July 17th, 2010 @ 07:07 PM

  2. Bo Kirkeby
    Bo Kirkeby

    Yeah, #model works great. I was trying to do something like:

    Given a user: "Fred" exists
    When I delete the user: "Fred"
    Then the user: "Fred" should not exist
    

    And this does not work as I expect if another user exists because the generated pickle step matching Then the user: "Fred" should not exist uses #find_model.

    I could tweak the step definition to do what I want with #model, but I thought #find_model was supposed to use the label also.

    Thanks for your time,
    Bo

    July 17th, 2010 @ 07:42 PM

  3. Ian White
    Ian White

    Hi Bo,

    Remember that the pickle refs are purely for internal referencing in a scenario.

    With that in mind, You probably want to use an attribute on the model to find/create the user:

    Given a user "Fred" exists with name: "fred"
    When I delete the user "Fred"
    Then a user should not exist with name: "fred"

    Cheers,
    Ian

    July 17th, 2010 @ 07:45 PM

  4. Ian White
    Ian White

    Thinking about it some more, your use case illustrates that an error should be raised if you are asserting non-existence of models, and giving a name

    EIther that, or make two steps, one which queries the db, and one which queries via pickle

      Then the user: "fred" should not exist
      Then the user should not exist
      #=> model(pickle_ref).should be_nil
    
      Then 0 users should exist with name: "fred"
      Then no users should exist with name: "fred"
      #=> look in the db
    

    Cheers,
    Ian

    July 17th, 2010 @ 08:08 PM

  5. Bo Kirkeby
    Bo Kirkeby

    Alright, I guess I misunderstood the intent of #find_model.

    Do you think it is wrong to test non-existence of a model using a label instead of by attributes?
    I could do something like this in the step definition:

    lambda { model(name) }.should raise_error(ActiveRecord::RecordNotFound)
    

    It seems to work correctly, but is it using the pickle reference for something that was really not intended?

    July 18th, 2010 @ 11:02 AM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.