Forum: RSpec fixtures and associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-17 19:47
(Received via mailing list)
Hello... I want ask for a help in error that don't understand.

I have properties model and tenants model. Tenant belongs to Property
and
Property has many Tenants... I try this test association but allways get
"not valid" error for association. Anyone can help? I post all my
classes
below:

My Tenant class:
class Tenant < ActiveRecord::Base
  belongs_to :property

  validates_associated  :property
  validates_presence_of :property
  ...

My tenant_spec.rb
describe Tenant do
  fixtures :properties

  def create_tenant(options = {})
    record = Tenant.create({
      :property => properties(:two),
      :renters => "value for renters",
      :guarantor => "value for guarantor",
      :contact_name => "value for contact_name",
      :contact_phone => "value for contact_phone",
      :contact_email => "teste@hosttest.com",
      :end_date => Date.today,
      :beginning_date => Date.today,
      :initial_value => "9.99"
    }.merge(options))
  end

  before(:each) do
    @tenant = create_tenant
  end

  it "should create a new instance given valid attributes" do
    @tenant.errors.should be_blank
  end
...

My property_spec.rb
class Property < ActiveRecord::Base
  has_many :tenants
...

My properties.yml:
two:
  user: aaron
  owner: tania
  address: Rua de teste
  address_number: 250
  neighborhood: Bairro ABC
  country_state: MG
  country: Brasil
  city: Belo Horizonte
  cep: 30566500
  area: 150m
  constructed_area: 120m
  category: galpão
  usage: comercial
  owner_code: AS341ADV
  garage: 2
  description: Casa com 3 quartos
  observation:
  condominium_value: 0
  iptu_value: 90.50


Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-12-17 20:31
(Received via mailing list)
On Wed, Dec 17, 2008 at 12:37 PM, Daniel Lopes <danielvlopes@gmail.com>
wrote:
>   validates_presence_of :property
>       :contact_phone => "value for contact_phone",
>
>   user: aaron
>   category: galpão
>   usage: comercial
>   owner_code: AS341ADV
>   garage: 2
>   description: Casa com 3 quartos
>   observation:
>   condominium_value: 0
>   iptu_value: 90.50

Hi Daniel,

Are both tenant_spec and property_spec failing? What are the failure
messages you're getting?
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-17 20:42
(Received via mailing list)
Hello David, the error is only in tenant_spec ... in property_spec all
my
examples passed. The error is in "@tenant.errors.should be_empty" line
and
the message is expected empty? to return true, got false
If I use puts to print my @tenant object before the assertion I get
those
values:

#<ActiveRecord::Errors:0x23aabf4
@errors={"property"=>["is not valid"]},
@base=#<Tenant id: nil,
property_id: 996332877,
renters: "value for renters",
guarantor: "value for guarantor",
contact_name: "value for contact_name",
contact_phone: "value for contact_phone",
end_date: "2008-12-17",
beginning_date: "2008-12-17",
initial_value: #<BigDecimal:239a4fc,'0.999E1',8(8)>,
created_at: nil,
updated_at: nil,
contact_email: "teste@hosttest.com">>

Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-12-17 21:05
(Received via mailing list)
Hi Daniel,

What happens when you do

properties(:two).valid?
p properties(:two).errors

It looks like that fixture is not valid.

Pat

"Daniel Lopes" <danielvlopes@gmail.com> writes:
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-17 21:30
(Received via mailing list)
I get cannot be null error user in active record object... like below:
 properties(:two).valid? return false, but I don't understand why...
because
my properties.yml file is like below:

two:
  user: aaron
  owner: tania
  address: Rua de teste
  address_number: 250
  neighborhood: Bairro ABC
...

and my users.yml is:

aaron:
  id:                        2
  login:                     aaron
  name:                      aaron lorem
  email:                     aaron@rspectest.com
...

But my activerecord object return this:

#<ActiveRecord::Errors:0x239be10 @errors={"user"=>["can't be blank"]},
@base=#<Property id: 996332877,
user_id: 317248456,
owner_id: 465176508,
address: "Rua de teste",
address_number: "250",
neighborhood: "Bairro ABC",
country: "Brasil",
city: "Belo Horizonte",
area: "150m",
constructed_area: "120m",
category: "galpão",
usage: "comercial",
owner_code: "AS341ADV",
condominium_value: #<BigDecimal:2389274,'0.0',4(8)>,
iptu_value: #<BigDecimal:2389238,'0.905E2',8(8)>,
garage: 2,
description: "Casa com 3 quartos",
observation: nil,
cep: "30566500",
country_state: "MG",
created_at: "2008-12-17 20:11:11",
updated_at: "2008-12-17 20:11:11",
zoning: "z10" >>

But it is not blank. What i'm doing wrong on my fixtures files? I think
I
don't get the message yet in other model spec because this is the fist
model
that use properties.yml ( in properties_spec I only use fixtures for
users )

Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-12-17 22:18
(Received via mailing list)
You are loading the users fixtures.  You need to change your fixtures
line to read

fixtures :properties, :users

(and any other models' fixtures you want to use)

I highly recommend ditching fixtures all together.  You avoid funky shit
like this, and you won't have coupling between tests that occurs with
fixtures, and you'll be able to create more clear specs because you do
all the setup in the spec itself.  If you're interested, look into
something like fixtures_replacement, factory girl, object daddy, etc.
Personally, I use a little 15-line homebrew framework.

Pat


"Daniel Lopes" <danielvlopes@gmail.com> writes:
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-17 23:35
(Received via mailing list)
Hello Pat, even I load all my fixtures it still not reading anything
from
users.yml ...
I will look at factory_girl but before I need understand what happening.

I try this:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Tenant do
  fixtures :properties, :users , :owners

  def create_tenant(options = {})
    record = Tenant.create({
      :property => properties(:two),
      :renters => "value for renters",
      :guarantor => "value for guarantor",
      :contact_name => "value for contact_name",
      :contact_phone => "value for contact_phone",
      :contact_email => "teste@hosttest.com",
      :end_date => Date.today,
      :beginning_date => Date.today,
      :initial_value => "9.99"
    }.merge(options))
  end

  before(:each) do
    @tenant = create_tenant
  end

  it "should create a new instance given valid attributes" do
    p properties(:two).errors
    properties(:two).should be_valid
    # @tenant.errors.should be_empty
  end

end




Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2008-12-18 00:43
(Received via mailing list)
On 17 Dec 2008, at 21:07, Pat Maddox wrote:

> I highly recommend ditching fixtures all together.  You avoid funky
> shit
> like this, and you won't have coupling between tests that occurs with
> fixtures, and you'll be able to create more clear specs because you do
> all the setup in the spec itself.  If you're interested, look into
> something like fixtures_replacement, factory girl, object daddy, etc.

+1

You'll never look back.


Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-12-18 01:17
(Received via mailing list)
On Wed, Dec 17, 2008 at 2:13 PM, Daniel Lopes <danielvlopes@gmail.com>
wrote:
>       :renters => "value for renters",
>   before(:each) do
>     @tenant = create_tenant
>   end
>
>   it "should create a new instance given valid attributes" do
>     p properties(:two).errors
>     properties(:two).should be_valid
>     # @tenant.errors.should be_empty
>   end
> end

What error are you getting now?  Please post whatever error you're
experiencing...when we change this code, I expect a different error,
and would not know what it is.  If it's the same, that's strange.

Also, I noticed you have
p properties(:two).errors
properties(:two).should be_valid

That's not going to show you anything, because errors don't get added
until you call #valid?  So change that to

properties(:two).valid?
p properties(:two).errors

Pat
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-18 01:41
(Received via mailing list)
Hello Pat, the error still the same.@errors={"user"=>["can't be blank"]}

But if we look on @base we have user:
@base=#<Property id: 996332877, user_id: 317248456, owner_id: 465176508,
...
>

And my code is:
describe Tenant do
  fixtures :properties, :users ,:owners

  def create_tenant(options = {})
    record = Tenant.create({
      :property => properties(:two),
      :renters => "value for renters",
      :guarantor => "value for guarantor",
      :contact_name => "value for contact_name",
      :contact_phone => "value for contact_phone",
      :contact_email => "teste@hosttest.com",
      :end_date => Date.today,
      :beginning_date => Date.today,
      :initial_value => "9.99"
    }.merge(options))
  end

  before(:each) do
    @tenant = create_tenant
  end

  it "should create a new instance given valid attributes" do
    properties(:two).valid?
    p properties(:two).errors
    # @tenant.errors.should be_empty
  end

And my fixtures still the same... I don't know what's happening, when I
try
run on console or in browser everything ok... I think is something
strange
with fixtures.

Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2008-12-18 01:46
(Received via mailing list)
On Thu, Dec 18, 2008 at 1:38 AM, Daniel Lopes
<danielvlopes@gmail.com>wrote:

>   def create_tenant(options = {})
>     record = Tenant.create({
>

Just a small tip. Always, always use create! in specs. You want an error
to
be raised in case creation is unsuccessful, and create doesn't do that -
it
just returns false.

Aslak
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-18 02:46
(Received via mailing list)
I know... I just don't use create!, in this special case, because it
will be
more complicate to know what field have the error .
I'm still scratching my head, my @tenant instance var on spec has values
for
user_id and owner_id and don't understand why my validates_associated
:property raise an error.

And if I mock model on model spec will not be a good thing.

Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501


On Wed, Dec 17, 2008 at 10:45 PM, aslak hellesoy
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2008-12-18 03:14
(Received via mailing list)
On Thu, Dec 18, 2008 at 2:02 AM, Daniel Lopes
<danielvlopes@gmail.com>wrote:

> I know... I just don't use create!, in this special case, because it will
> be more complicate to know what field have the error .
>

No it won't. The error message tells you what field has the error, and
if
creation is unsuccessful you *need* to know about it. Otherwise you'll
get
tons of false positives. I can't think of a single case where it makes
sense
to use create inside a spec.

Aslak
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-18 03:49
(Received via mailing list)
I think I was misunderstood. I am not saying to use create rather than
create! ... In this case, I just use it because will help me with puts
for
debug my own error in spec.
In this case a really don't know what is going on, if I use create! I
can't
store the value in a var and then try to look what is the message of the
failed attribute with puts.
I using puts to print the error object to debug who is the wrong
attribute.
If I use create! I will have this message:
Validation failed: Property is not valid => and cant use puts to know
the
content of @errors

If I use create and print the result of fixture I will get:

<ActiveRecord::Errors:0x23af4b0 @errors={"user"=>["não pode ser
vazio"]},
@base=#<Property id: 996332877, user_id: 317248456, ...

And find the problem, it is just for debugging and help me to find where
is
the problem, after fix this I will change to create! again.

--

Anyone can figure why even property_id and owner_id are defined still
raise
"can't be blank" ?

Thanks a lot for all help.

Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501


On Thu, Dec 18, 2008 at 12:10 AM, aslak hellesoy
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-18 16:49
(Received via mailing list)
I really don't know what's going on with those hell fixtures, and start
thinking I will never find... I will try migrate all my fixtures to
factory-girl, the only problem are my six hundred examples and no time
to
change all. Somebody have any link about rspec + factory-girl ? or maybe
link to any project on github that use factory-girl and rspec ?
Thanks for all help.

Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-12-18 17:33
(Received via mailing list)
On Thu, Dec 18, 2008 at 7:24 AM, Daniel Lopes
<danielvlopes@gmail.com>wrote:

> I really don't know what's going on with those hell fixtures, and start
> thinking I will never find... I will try migrate all my fixtures to
> factory-girl, the only problem are my six hundred examples and no time to
> change all.


You don't have to change all your fixtures to factories right now, of
course, and, indeed, you should not. Just change the ones that are
giving
you problems for now; and later, maybe, change each on as you work on
it.

///
Ff937b6907db49432c980f2b6a5c7e71?d=identicon&s=25 Mischa Fierer (mischa)
on 2008-12-18 17:36
(Received via mailing list)
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-18 17:37
(Received via mailing list)
So, I think the problem is my fixture became crazy with id's and not
assign
the right id for the right object in association when I try use Rails 2
sintax for fixtures ( omitting the ids and using the name of the fixture
in
the association instead of id ) ... when I change everything to fixed id
in
my yml files everything works.
Crazy. I'm still want to migrate to factory-girl, if anybody has
material or
links on github about projects using rspec and factory-girl will help
alot.

Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
Ff937b6907db49432c980f2b6a5c7e71?d=identicon&s=25 Mischa Fierer (mischa)
on 2008-12-18 17:39
(Received via mailing list)
oops
that last one was supposed to be

http://github.com/ianwhite/pickle/tree/master

similar names
89e57a28dfdb85e07b33f92783dbe349?d=identicon&s=25 Daniel Lopes (Guest)
on 2008-12-18 18:44
(Received via mailing list)
Thanks everybody...
Atenciosamente,

Daniel Lopes    Area Criações
Design, Websites e Sistemas Web

Visite:  http://www.areacriacoes.com.br/projects
          http://blog.areacriacoes.com.br/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
*
55 (31) 3077-4560  /  55 (31) 8808-8748  /  55 (31) 8737-7501
This topic is locked and can not be replied to.