Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model and Test values not the same #40

Closed
Gerhardk opened this issue Oct 12, 2011 · 12 comments
Closed

Model and Test values not the same #40

Gerhardk opened this issue Oct 12, 2011 · 12 comments

Comments

@Gerhardk
Copy link

Hey I have a really weird problem which does not make any sense to me.
My environment is Factory_Girl_Rails(1.2.0) on jruby 1.6.3, rails 3.0.10
What the issue is I create a dummy object with factory_girl and some how the objects attributes are different within the tests and in the model.
What I mean by it is in the test the value of an attribute is 4.5 and in the model the same attribute is 5.0 on the same object.

Can some help me to determine if its a active record problem or factory_girl or something else?

Thanks

@joshuaclayton
Copy link
Contributor

Can you post the factory and the section of the db/schema.rb that creates its table?

@Gerhardk
Copy link
Author

Here is the factory:
Factory.define(:object) do |f|
f.name "Test Object"
f.set_spacing 4.5
end

Here is the schema.rb:
create_table "object", :force => true do |t|
t.string "name"
t.decimal "set_spacing"
end

In the schema.rb it says decimal but I run a migration which changes the type to float:
change_column :object, :set_spacing, :float, :precision => 8, :scale => 1

@joshuaclayton
Copy link
Contributor

If you create the model without factory girl, what happens?

define Object, "with spacing set" do
  subject { Object.create!(:set_spacing => 4.5) }
  its(:set_spacing) { should == 4.5 }
end

@Gerhardk
Copy link
Author

When I create the object without the factory the set_spacing is 4.5:
it "should give set_spacing as 4.5" do
object = object.create!(:set_spacing => 4.5, :name => "Test Object")
object.set_spacing.should == 4.5
end

@Gerhardk
Copy link
Author

When i do the following test the set_spacing is 5.0 and not 4.5:

it "should give set_spacing as 4.5" do
     object = Object.create!(:set_spacing => 4.5, :name => "Test Object")
      objects.set_spacing.should == 4.5
      object_action = ObjectAction.create( :name_past => "replaced", :object_counter => "", :object_name => "Pipe Section")
      job                = Job.create(:object_id => object.id)


      job.object_counter_string.should == "9.0m"
    end

It fails on the last line and give answer as "10.0m" meaning the set_spacing in the model side gets round to 5.0

@joshuaclayton
Copy link
Contributor

Have you overwritten the setter for set_spacing, per chance? When FactoryGirl assigns attributes, all it does is loop through each one and essentially call instance.send("#{attribute_name}=", attribute_value). What's returned when you call FactoryGirl.attributes_for(:object)?

@Gerhardk
Copy link
Author

Can it be that the following is overwriting the value as it comes into the model
validates :set_spacing, :presence => true, :numericality => true

@Gerhardk
Copy link
Author

The output of FactoryGirl.attributes_for(:object) is:
{:name=>"Test Object", :set_spacing=>4.5}

@joshuaclayton
Copy link
Contributor

Wonderful! That means it's not an issue with FactoryGirl. I'd look through your model code and try to trace down where the value is changing. I'd look in any callbacks, setters, or anything else that'd modify set_spacing. If you call FactoryGirl.build and it's set correctly to 4.5, but you know it doesn't work with FactoryGirl.create, it's pretty safe to assume it's somewhere in your validations, callbacks, or in the database layer. I'd also tail the test log to see what's actually inserted into the database, since that may help provide more clues.

@Gerhardk
Copy link
Author

Thanks for the help

@joshuaclayton
Copy link
Contributor

No problem - let me know what you end up finding!

@Gerhardk
Copy link
Author

I looked at the validations but nothing, next I went and looked at my callbacks nothing the only thing that I can think of is that my database which is Microsoft SQL Server 2008 is rounding the numbers.

I could not find any place where I change the set_spacing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants