#25 new
Bo Kirkeby

#find_model does not use label to find the model

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

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.

Comments and changes to this ticket

  • Ian White

    Ian White July 17th, 2010 @ 07:07 PM

    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

  • Bo Kirkeby

    Bo Kirkeby July 17th, 2010 @ 07:42 PM

    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

  • Ian White

    Ian White July 17th, 2010 @ 07:45 PM

    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

  • Ian White

    Ian White July 17th, 2010 @ 08:08 PM

    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

  • Bo Kirkeby

    Bo Kirkeby July 18th, 2010 @ 11:02 AM

    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?

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Assign tickets to <b>Ian White</b>, so I get notified

People watching this ticket

Pages