Questions tagged [rspec]

RSpec is a behavior-driven development (BDD) framework for the Ruby programming language, inspired by JBehave. It contains its own fully integrated mocking framework based upon JMock. The framework can be considered a domain-specific language (DSL) and resembles a natural language specification.

RSpec is a behaviour-driven development (BDD) tool for Ruby programmers. BDD is an approach to software development that combines test-driven development (TDD), domain-driven design (DDD), and acceptance test-driven planning (ATDP). RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

RSpec 2 and later site

RSpec 1 site

Resources

Books

18192 questions
85
votes
5 answers

Running ruby debug in rspec?

I'm trying to get Ruby debugger running in one of my specs: describe User do it "should be valid" do debugger User.new.should be_valid end end When I run rspec though, I get: debugger statement ignored, use -d or --debug option to…
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
85
votes
2 answers

Rubocop 25 line block size and RSpec tests

A typical RSpec unit test makes extensive use of nested Ruby blocks in order to structure the code and make use of DSL "magic" to have specs read like BDD statements: describe Foo do context "with a bar" do before :each do subject {…
Neil Slater
  • 26,512
  • 6
  • 76
  • 94
85
votes
7 answers

RSpec: how to test Rails logger message expectations?

I am trying to test that the Rails logger receives messages in some of my specs. I am using the Logging gem. Let's say that I have a class like this: class BaseWorker def execute logger.info 'Starting the worker...' end end And a spec…
keruilin
  • 16,782
  • 34
  • 108
  • 175
83
votes
8 answers

What is the best way to write specs for code that depends on environment variables?

I am testing some code that pulls its configuration from environment variables (set by Heroku config vars in production, for local development I use foreman). What's the best way to test this kind of code with RSpec? I came up with this: before…
Luke Francl
  • 31,028
  • 18
  • 69
  • 91
83
votes
8 answers

How to make Capybara check for visibility after some JS has run?

After loading a page I have code that runs and hides and shows various items based on data returned by an xhr. My integration test looks something like this: it "should not show the blah" do page.find('#blah').visible?.should be_true end When…
Kevin Davis
  • 2,698
  • 1
  • 22
  • 27
83
votes
2 answers

What is the purpose of a `transient do` block in FactoryBot factories?

What is the purpose of transient do in FactoryBot factories? I've seen a lot of factories that begin with something like below. factory :car do owner nil other_attribute nil end ... I've found some information on this blog: Using FactoryGirl to…
tim_xyz
  • 11,573
  • 17
  • 52
  • 97
82
votes
9 answers

Ruby on Rails: Switch from test_unit to rspec

I'm going through a tutorial that has suggested using rspec, but I have already gone through a lot of default rails installation. I really don't want to have to redo the installation at all. Anyway, when I run $ rails g integration_test named I…
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
82
votes
5 answers

How to define an array / hash in factory_bot?

I am trying to write a test that simulates some return values from Dropbox's REST service that gives me back data in an Array, with a nested hash. I am having trouble figuring out how to code my Factory since the return result is an array with a has…
Doug
  • 14,387
  • 17
  • 74
  • 104
80
votes
7 answers

How to say "any_instance" "should_receive" any number of times in RSpec

I've got an import controller in rails that imports several csv files with multiple records into my database. I would like to test in RSpec if the records are actually saved by using…
Harm de Wit
  • 2,150
  • 2
  • 18
  • 24
80
votes
3 answers

Rspec Output Format: Documentation

When I run rspec with rake rspec and my tests are not ok, I get an error message. However, when my tests are ok, I just get '..'. No other output. How can I get it to print something like: A User .... can only have one name A User .... can ...
Spyros
  • 46,820
  • 25
  • 86
  • 129
79
votes
10 answers

How do I change the default "www.example.com" domain for testing in rails?

I have a rails application which acts differently depending on what domain it's accessed at (for example www.myapp.com will invoke differently to user.myapp.com). In production use this all works fine but my test code always sees a hostname of…
Denis Hennessy
  • 7,243
  • 4
  • 25
  • 31
78
votes
2 answers

Object.any_instance should_receive vs expect() to receive

The following piece of code works as expected: Object.any_instance.should_receive(:subscribe) But when using the new rspec expectation it does not work: expect(Object.any_instance).to receive(:subscribe) The error is: expected: 1 time with any…
Calin
  • 6,661
  • 7
  • 49
  • 80
77
votes
11 answers

Suppress Ruby warnings when running specs

I'm looking for a way to suppress Ruby warnings when I run my specs. spec spec/models/account_spec.rb I receive warnings such as: DEPRECATION WARNING: ActiveSupport::Dependencies.load_paths is deprecated, ... warning: already initialized constant…
Jey Balachandran
  • 3,585
  • 5
  • 27
  • 36
76
votes
7 answers

Rails/Rspec Make tests pass with http basic authentication

Here's my http basic authentication in the application controller file (application_controller.rb) before_filter :authenticate protected def authenticate authenticate_or_request_with_http_basic do |username, password| username == "username"…
benoitr
  • 6,025
  • 7
  • 42
  • 67
76
votes
4 answers

FactoryBot: create the same object multiple times

In one of my RSpec test, I am creating multiple objects from the same factory definition Eg FactoryBot.create(:model_1) FactoryBot.create(:model_1) FactoryBot.create(:model_1) Is there a method that factory_bot provides to do this in one line I…
usha
  • 28,973
  • 5
  • 72
  • 93