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
125
votes
4 answers

How to test exception raising in Rails/RSpec?

There is the following code: def index @car_types = car_brand.car_types end def car_brand CarBrand.find(params[:car_brand_id]) rescue ActiveRecord::RecordNotFound raise Errors::CarBrandNotFound.new end I want to test it through RSpec.…
malcoauri
  • 11,904
  • 28
  • 82
  • 137
122
votes
1 answer

What's the difference between RSpec and Cucumber?

I have 6 months of Rails development experience. I've built a web application that's in use now with authentication and authorization and postgresql db. I'm moving on to my second Rails application but this time, after lessons learnt, I would like…
banditKing
  • 9,405
  • 28
  • 100
  • 157
119
votes
1 answer

RSpec: What is the difference between a feature and a request spec?

What is the conceptual difference between Rspec's feature specs and request specs? From the feature spec docs: Feature specs are high-level tests meant to exercise slices of functionality through an application. They should drive the application…
harm
  • 10,045
  • 10
  • 36
  • 41
117
votes
7 answers

Disable a group of tests in rspec?

I have a test spec which describes a class and within that has various contexts each with various it blocks. Is there a way I can disable a context temporarily? I tried adding a pending "temporarily disabled" call at the very top within a context I…
Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
117
votes
1 answer

rspec 3 - stub a class method

I am upgrading from rspec 2.99 to rspec 3.0.3 and have converted instance methods to use allow_any_instance_of, but haven't figured out how to stub a class method. I have code like this: module MyMod class Utils def self.find_x(myarg) #…
Peter Sankauskas
  • 2,882
  • 4
  • 27
  • 28
113
votes
7 answers

Rails 4: How to reset test database?

I'm on Rails 4 and have noticed some of my RSpec tests are failing because some of my test refactorings use a before filter (presumably because of transactions). This post describes a similar issue: rails test database not clearing after some…
Solomons_Ecclesiastes
  • 2,039
  • 3
  • 14
  • 17
113
votes
8 answers

Rspec doesn't see my model Class. uninitialized constant error

I'm writing tests on Rspec for my models in Ruby on Rails application. And I receive this error while starting 'rspec spec' command: /spec/models/client_spec.rb:4:in `': uninitialized constant Client (NameError) I use Rails 4.0.0…
Stan
  • 1,267
  • 2
  • 8
  • 11
109
votes
4 answers

RSpec controller testing - blank response.body

I am stuck with a problem when testing my controllers with RSpec - the response.body call always returns an empty string. In browser everything renders correctly, and cucumber feature tests seem to get it right, but RSpec fails each and every…
Toms Mikoss
  • 9,097
  • 10
  • 29
  • 41
104
votes
9 answers

Capybara Ambiguity Resolution

How do I resolve ambiguity in Capybara? For some reason I need links with the same values in a page but I can't create a test since I get the error Failure/Error: click_link("#tag1") Capybara::Ambiguous: Ambiguous match, found 2 elements…
neilmarion
  • 2,372
  • 7
  • 21
  • 36
102
votes
7 answers

How to set up factory in FactoryBot with has_many association

Can someone tell me if I'm just going about the setup the wrong way? I have the following models that have has_many.through associations: class Listing < ActiveRecord::Base attr_accessible ... has_many :listing_features has_many :features,…
Tonys
  • 3,349
  • 4
  • 24
  • 27
102
votes
6 answers

Can RSpec stubbed method return different values in sequence?

I have a model Family with a method location which merges the location outputs of other objects, Members. (Members are associated with families, but that's not important here.) For example, given member_1 has location == 'San Diego (traveling,…
Mike Blyth
  • 4,158
  • 4
  • 30
  • 41
100
votes
6 answers

How to get rspec-2 to give the full trace associated with a test failure?

Right now if I run my test suite using rake spec I get an error: 1) SegmentsController GET 'index' should work Failure/Error: get 'index' undefined method `locale' for # # ./spec/controllers/segments_controller_spec.rb:14: in `block…
marcgg
  • 65,020
  • 52
  • 178
  • 231
100
votes
2 answers

How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

I am doing the Rails Tutorial for the second time. When I enter this rails generate integration_test static_pages I get spec/rails_helper.rb and spec/spec_helper.rb instead of just spec/spec_helper.rb Now when I run my tests, they are longer (more…
user3417583
  • 1,417
  • 3
  • 11
  • 13
99
votes
8 answers

How to get Rails.logger printing to the console/stdout when running rspec?

Same as title: How to get Rails.logger printing to the console/stdout when running rspec? Eg. Rails.logger.info "I WANT this to go to console/stdout when rspec is running" puts "Like how the puts function works" I still want Rails.logger to go to…
s12chung
  • 1,718
  • 1
  • 14
  • 29
97
votes
12 answers

Factory-girl create that bypasses my model validation

I am using Factory Girl to create two instances in my model/unit test for a Group. I am testing the model to check that a call to .current returns only the 'current' groups according to the expiry attribute as per below... describe ".current" do …
Norto23
  • 2,249
  • 3
  • 23
  • 40