[rspec-users] fixtures and associations

Pat Maddox pergesu at gmail.com
Wed Dec 17 16:07:42 EST 2008


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 at gmail.com> writes:

> 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 at 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
>
> On Wed, Dec 17, 2008 at 6:02 PM, Pat Maddox <pergesu at gmail.com> wrote:
>
>     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 at gmail.com> writes:
>    
>     > 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 at 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
>     >
>     > On Wed, Dec 17, 2008 at 5:18 PM, David Chelimsky <dchelimsky at gmail.com> wrote:
>     >
>     >     On Wed, Dec 17, 2008 at 12:37 PM, Daniel Lopes <danielvlopes at gmail.com> wrote:
>     >     > 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 at 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
>     >
>     >     Hi Daniel,
>     >
>     >     Are both tenant_spec and property_spec failing? What are the failure
>     >     messages you're getting?
>     >
>     >     >
>     >     > 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
>     >     _______________________________________________
>     >     rspec-users mailing list
>     >     rspec-users at rubyforge.org
>     >     http://rubyforge.org/mailman/listinfo/rspec-users
>     >
>     > _______________________________________________
>     > rspec-users mailing list
>     > rspec-users at rubyforge.org
>     > http://rubyforge.org/mailman/listinfo/rspec-users
>     _______________________________________________
>     rspec-users mailing list
>     rspec-users at rubyforge.org
>     http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list