#rspec

2025-07-07

TIL in ‘describe’ blocks are evaluated when the file is parsed, not when the spec is executed. That one took me a few hours! :yikes: :michaelwazowski:

2025-06-23

Is there a RSpec matcher to expect that a value exists in an Array of expected values? If not, there should be. I don't like inverting my expect() statement to be:

expect(expected_values).to include(subject.thing_i_am_testing) # not ideal

Ideally it would look like:

expect(subject.foo).to eq_any_of(expected_values) # better

#rspec

Bradley Schaefersoulcutter@ruby.social
2025-05-06

Heads up: rspec-mocks 3.13.3 (released May 1) is a BAD version.

There's something wrong with verifying mocks that seems like it causes a problem with mocking methods that take keyword arguments and a block. It seems like the vast majority of mocking works, just some kind of nesting edge case.

rspec-mocks 3.13.4 (released May 5) fixes the apparent problem

I can't immediately spot what it was, something about the invoking flag

github.com/rspec/rspec/compare

cc @JonRowe

#ruby #rspec

Aaron Sumnerruralocity
2025-05-06

Hey friends! Just a reminder that this sale is going on all month. ❤️ to all your support these past 15 years. (And if $9 is out of your budget, DM me for a freebie link, no questions asked)
mastodon.social/@ruralocity/11

Aaron Sumnerruralocity
2025-05-06

Anyone have better approaches to wiping Active Storage files after test runs in ?

```
config.after(:suite) do
dir = ActiveStorage::Blob.services.fetch(:test).root
FileUtils.rm_rf(dir)
FileUtils.mkdir_p(dir)
FileUtils.touch(File.join(dir, ".keep"))
end
```

Aaron Sumnerruralocity
2025-05-05

Welp, my improptu 15th anniversary sale became a "just needed to buy a new washer/dryer" sale 😭 $9 for lifetime free updates to my book! Sharing is caring! leanpub.com/everydayrailsrspec

Jay McGavrenjaymcgavren
2025-05-03

The output of RSpec's "expect(thing).to recieve(:act).with('foo')" can be a pain to debug if "act" is called with things other than "foo". But TIL combining "expect" with "allow" lists the calls:

class Dog
def say(sound); end
end

it "says bark" do
dog = Dog.new
allow(dog).to receive(:say)
expect(dog).to receive(:say).with("bark")
dog.say("meow")
dog.say("oink")
end

# Failure message:
expected:
("bark")
got:
("meow") (1 time)
("oink") (1 time)

2025-04-25

#Ruby friends! I have a large number #rspec files. I added Sorbet and I want to have a rough idea if things are good or not before submitting to CI.

I'd like to run specs, but let's say 10% of specs within files.

Does anyone have a quick something already done? Or should I add a before block and cancel the example if rand > 0.1?

Cédric Delalande :ruby:MoskitoHero@ruby.social
2025-04-17

Made a small gem to allow switching from class to test - and back - in #zed

github.com/MoskitoHero/zed-tes

It’s still at an early stage, so I suspect it might not work in some projects. But it understands if you are using #rspec or #minitest.

#ruby #gem

Aaron Sumnerruralocity
2025-04-07

💥 I shipped a new chapter for the current edition of Everyday Rails Testing with ! I’ve overhauled my introduction to testing in isolation with mocks (and stubs, and fakes, and spies, and doubles, oh my). 💥

Free update on Leanpub: leanpub.com/everydayrailsrspec/

Aaron Sumnerruralocity
2025-03-30

pals—I'm not super in-the-loop at the moment, but is there a ballpark on when Rails 8.1 is going to drop? For context, I'm thinking about pausing on content updates on my book, do a quick 8.0 update for the chapters done so far, then wrap up the remaining chapters.

Aaron Sumnerruralocity
2025-03-23

Updating coverage of the Ruby "VCR" mocking package in my book, and thinking how many younger people have likely never even seen a VCR, much less a TiVo—which then reminded me of the behemoth ReplayTV that sat under my cathode ray television in the aughts. 👴

Bradley Schaefersoulcutter@ruby.social
2025-03-20

I got to pull out a nice RSpec trick today - using compound expectations.

I wanted to check that a method was called with a (rather large) string that had a few different substrings in it.

expect(instance).to receive(:method).with(
a_string_including("thing 1") &
a_string_including(obj.message) &
a_string_including("asdfasdfgasd")
)

The trick here is combining matchers with &

There's other ways you might do this, but the test output is really nice this way.

#ruby #rspec

Aaron Sumnerruralocity
2025-03-09

Finally got an update to my book for shipped this afternoon, with new content on tools for DRYer tests, and when they're a good idea or a bad idea. Free update as usual! leanpub.com/everydayrailsrspec

Aaron Sumnerruralocity
2025-03-06

Reposting for western hemisphere daytime crowd. Boosts appreciated!

Aaron Sumnerruralocity
2025-03-06

Heya fans, especially users: Feelings about testing at a single level of abstraction? Debating whether to include it again (with revisions) in my RSpec book updates. I like it in principle; is the extra setup worth the improved readability to you? glennespinosa.com/ruby/refacto

Peter Solnicasolnic@hachyderm.io
2025-02-24

One more prompt and we found the culprit 🙃 Now I just asked it to find which specific spec scenario causes our failure and 💥 got it, fixed it, and now I also know that there's state leaking between specs which I want to address in a generic way. Win!

#aicoding #testing #rspec #debugging #ruby

A prompt from Cursor AI editor asking it to write a script that will find combinations for all test scenarios from one file and run them along with another file to find the combination that causes failure in that other fileTerminal showing output from running the script that found the offending scenario
2025-02-14

🎙️New Episode Alert!🎙️

@srbaker and Philippe joined me to dive deep into testing and RSpec. We covered best practices, common pitfalls, and shared some real-world insights that every developer can relate to.

If you're into writing better tests (or just want to hear some passionate devs chat about it), this one's for you!

Listen here: open.spotify.com/episode/08C0z

Let me know what you think!

#Podcast #RSpec #Testing #TechTalk #RubyOnRails

Bradley Schaefersoulcutter@ruby.social
2025-02-12

Category of totally random. OSX Mojave doesn't play nice with `rspec --bisect` anymore because it crashes out when forking, like so:

objc[49901]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.

My colleague told me to try running with the environment variable `OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES` and it works.

OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES rspec --bisect --seed 1234

What is this magic?

#ruby #osx #rspec

Bradley Schaefersoulcutter@ruby.social
2025-02-11

My favorite RSpec workflow is using the --next-failure option.

1. Run your entire test suite to generate the list of failing specs
2. Run `rspec --next-failure`
3. Debug and fix failure, continue repeating step 2 until the test passes and you have a new failure
4. Step 2 succeeds? Run the entire test suite again to ensure none of the fixes broke something else

You can plow through a lot of broken tests that way. I fixed 50 failing tests in about an hour yesterday by this method.

#ruby #rspec

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst