---
title: "Bogus and Class Methods"
url: "https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/bogus-and-class-methods.md"
canonical: "https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/bogus-and-class-methods/"
tags: []
author: "Roy Osherove"
description: "Lesson 9 of 15 in TDD and BDD in Ruby (Day 3 - Advanced RSpec and Faking), 2:05. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Bogus and Class Methods

1. [Courses](https://courses.osherove.com/#courses)
2. [TDD and BDD in Ruby](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/)
3. Day 3 - Advanced RSpec and Faking

Lesson 9 of 15

Duration: 2:05 [Watch on YouTube](https://www.youtube.com/watch?v=gUmc3WetXbs)

Learn how to [fake](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-5#ch05lev1sec2 "Dynamically creating a fake object (The Art of Unit Testing, 2nd ed., ch. 5)") class methods (statics) with Bogus.

## In this lesson

Roy Osherove shows how Bogus [fakes](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-4#ch04lev1sec2 "The difference between mocks and stubs (The Art of Unit Testing, 2nd ed., ch. 4)") class methods, which are almost like static methods in other languages, by using fake class on a shared logger and asserting that it should have received write. He adds that the testing tool can give hints about the design but should not dictate it.

## Transcript

294 words

[\[00:02\]](https://www.youtube.com/watch?v=gUmc3WetXbs&t=2s) Okay. So what I want to explain now is what happens when you have methods that are class methods instead of instance methods. So in this case what we have if we go to a shared logger we have a method called self.write. Now if we want to [fake](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-4#ch04lev1sec2 "The difference between mocks and stubs (The Art of Unit Testing, 2nd ed., ch. 4)") that method we can't just fake it on an instance because it's almost like a static method in other languages. So in this case we will use Bogus' abilities to fake class whole classes

[\[00:33\]](https://www.youtube.com/watch?v=gUmc3WetXbs&t=33s) By using fake class here. So you can just use the fake class construct by saying fake class shared logger and then to assert that a method on the shared a self method or a class method was called we can just say shared logger should have received write with the standard syntax. So instead of using the fake instance we just call the static method or the shared method which should and it should just work

[\[01:07\]](https://www.youtube.com/watch?v=gUmc3WetXbs&t=67s) Questions. That's one of the nice things that I like about dynamic languages is that they allow you to very very easily take over things which in other languages you would have a lot of problems. Now some people might say might you want that you might not not want to have class methods in the first place but I think that this is more of a design thing.

[\[01:37\]](https://www.youtube.com/watch?v=gUmc3WetXbs&t=97s) What we're what I'm trying to teach here is how to have good test specs and that shouldn't interfere with good design practices. You shouldn't let the the testing tool dictate exactly how your design looks. You can let it give you hints, but it's ultimately your choice how the design should look. So that's basically how you would work with the class methods. Okay.

## Related

- [Is Typemock too powerful? Will it kill design for testability?](https://osherove.com/blog/2008/1/17/is-typemock-too-powerful-will-it-kill-design-for-testability.html) · osherove.com

    Roy's post on whether a framework that can fake statics hides the design problems tests flag, the same point as 'the tool gives hints but should not dictate the design'.

- [Pros and cons of designing for testability](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-11#ch11lev1sec3) · The Art of Unit Testing, 2nd ed., ch. 11

    The 2nd edition's section on the pros and cons of designing for testability, background for faking class methods instead of redesigning.

- [Faking static methods in Moq, FakeItEasy and NSubstitute might be closer than imagined](https://osherove.com/blog/2012/7/8/faking-static-methods-in-moq-fakeiteasy-and-nsubstitute-migh.html) · osherove.com

    A .NET post on a technique for faking static methods in open-source frameworks; the tools differ, but the problem is the one Bogus solves for Ruby class methods here.

### Also linked in this lesson

#### The Art of Unit Testing, 2nd ed.

- [The difference between mocks and stubs](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-4#ch04lev1sec2), ch. 4
- [Dynamically creating a fake object](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-5#ch05lev1sec2), ch. 5

[Previous lesson Advanced RSpec and Cleaning Up The Specs](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/advanced-rspec-and-cleaning-up-the-specs/) [Next lesson Acceptance Tests with Cucumber](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/acceptance-tests-with-cucumber/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
