# Ruby Testing Technique - The Power and Stupidity of def foo

DevFeed: [Ruby Testing Technique - The Power and Stupidity of def foo](<https://devfeed.tech/articles/ruby-testing-technique-the-power-and-stupidity-of-def-foo-28300.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rspec/2020/01/03/ruby-testing-technique-the-power-and-stupidity-of-def-foo.html>)

Author: Fuzzygroup

Published: 2020-01-03T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Testing](<https://devfeed.tech/topics/testing.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Software Engineering](<https://devfeed.tech/topics/software-engineering.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [Database](<https://devfeed.tech/topics/database.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [database](<https://devfeed.tech/tags/database.md>), [factorybot](<https://devfeed.tech/tags/factorybot.md>), [rails](<https://devfeed.tech/tags/rails.md>), [rspec](<https://devfeed.tech/tags/rspec.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [testing](<https://devfeed.tech/tags/testing.md>), [validation](<https://devfeed.tech/tags/validation.md>)

## AI overview

A Ruby testing troubleshooting article describes debugging a failing FactoryBot factory for an Objective record in a Rails side project. The supplied excerpt identifies validation errors involving missing User and Organization associations and begins a technique of adding class and instance methods named foo to investigate the problem, but it ends before documenting the complete resolution.

## Source excerpt

I had an abysmal day yesterday coding on my side project. Ok - coding on one of my side projects. I had one of those days where you try to do something simple and NOTHING, NOT ONE DAMN THING, WORKS. Note: Every software engineer knows this type of day. They aren't days that you talk about with anyone in your life because you spend hours failing at something that you know is so damn simple that, if you can't make it work, you should honestly give up software engineering and go cut grass for a living. I refer to these days, when I have them, as spirit crushers / brain emasculators. And when they occur, I find that a nap (or a snickers bar) is the best medicine. All I was trying to do was make a simple factory work. For those who aren't deeply immersed in the world of software testing in ruby, a factory is a software method which creates a sample object that represents the actual object so it can be tested. Factories are used in place of actual objects, generally, because they run faster. Here was the definition of the factory: FactoryBot.define do factory :objective do name {Faker::Name.first_name} user organization objective_type okr_team quarter end end All this means is: Create an object named objective Give it a name attribute that is pulled from a library called Faker using the first_name method Give it a relationship back to the user object Give it a relationship back to the organization object Give it a relationship back to the objective_type object Give it a relationship back to the okr_team object Give it a relationship back to the quarter object Although this might seem complex, it is actually drop dead simple and something that I've probably done hundreds if not thousands of times. And yet, yesterday, all I could get was some variant on this censored backtrace: ActiveRecord::RecordInvalid: Validation failed: Organization must exist, User can't be blank, Organization can't be blank # /Users/sjohnson/.rvm/gems/ruby-2.6.5/gems/fabrication-2.21.0/lib/fabricatio