Comments (12)
Can you post the factory and the section of the db/schema.rb that creates its table?
from factory_bot_rails.
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
from factory_bot_rails.
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
from factory_bot_rails.
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
from factory_bot_rails.
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
from factory_bot_rails.
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)
?
from factory_bot_rails.
Can it be that the following is overwriting the value as it comes into the model
validates :set_spacing, :presence => true, :numericality => true
from factory_bot_rails.
The output of FactoryGirl.attributes_for(:object) is:
{:name=>"Test Object", :set_spacing=>4.5}
from factory_bot_rails.
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.
from factory_bot_rails.
Thanks for the help
from factory_bot_rails.
No problem - let me know what you end up finding!
from factory_bot_rails.
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
from factory_bot_rails.
Related Issues (20)
- factory_bot_rails 6.0 checklist HOT 2
- Invalid DateTime.now field on factory creation
- Multiple Tests Fail on Vanilla `bundle exec rake` HOT 2
- Model generation ignores --force-plural HOT 3
- Rails ignoring factory HOT 2
- Install fewer gems on Travis HOT 1
- Incompatibility with Rails 6.0.3? HOT 2
- Perform DB migration will make factory_bot looking for methods in the wrong place HOT 2
- Successive associations issue when migrating to rails 6.1 HOT 3
- Switch from Travis CI to GitHub Actions
- Duplicate factories problem HOT 5
- Automatically populate attributes and types when generating factories for existing models HOT 2
- Cannot create Factory with multiple traits got (ArgumentError Exception: wrong number of arguments (given 1, expected 0)) Ruby 3.0, Rails 6.1.72, factory_bot_rails 6.1.0 HOT 2
- Dead link in CONTRIBUTING.md HOT 2
- Release new version to allow upgrade to factory_bot 6.3.0 HOT 4
- The railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" Factory Bot > 6.2.0 HOT 5
- 6.3.0: `NoMethodError: undefined method '<' for nil:NilClass` when using a factory with traits HOT 20
- undefined method `config' for nil:NilClass after upgrading to 6.4.0 HOT 16
- Ability to disable reject_primary_key_attributes per factory HOT 11
- RubyGems is not linking the CHANGELOG file
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from factory_bot_rails.