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
154
votes
6 answers

How to get current path with query string using Capybara

The page url is something like /people?search=name while I used current_path method of capybara it returned /people only. current_path.should == people_path(:search => 'name') But it fails saying expected: "/people?search=name" got: "/people" How…
kriysna
  • 6,118
  • 7
  • 30
  • 30
144
votes
4 answers

RSpec: how to test if a method was called?

When writing RSpec tests, I find myself writing a lot of code that looks like this in order to ensure that a method was called during the execution of a test (for the sake of argument, let's just say I can't really interrogate the state of the…
Mikey Hogarth
  • 4,672
  • 7
  • 28
  • 44
142
votes
7 answers

How to click first link in list of items after upgrading to Capybara 2.0?

How to click first link in that case: within ".item" do first(:link, "Agree").click end and I get this error: Capybara::Ambiguous: …
tomekfranek
  • 6,852
  • 8
  • 45
  • 80
138
votes
10 answers

How to set request headers in rspec request spec?

In the controller spec, I can set http accept header like this: request.accept = "application/json" but in the request spec, "request" object is nil. So how can I do it here? The reason I want to set http accept header to json is so I can do…
Sergey
  • 4,702
  • 6
  • 26
  • 32
136
votes
6 answers

RSpec vs Cucumber (RSpec stories)

When should I use specs for Rails application and when Cucumber (former rspec-stories)? I know how both work and actively use specs, of course. But it still feels weird to use Cucumber. My current view on this, is that it's convenient to use…
orion3
  • 9,797
  • 14
  • 67
  • 93
135
votes
14 answers

How to check a checkbox in capybara?

I'm using Rspec and Capybara. How can I write a step to check a checkbox? I've tried check by value but it can't find my checkbox. I'm not sure what to do, as I have in fact same ID with different values Here is the code:
John Dow
  • 1,431
  • 2
  • 11
  • 8
133
votes
5 answers

How to Test a Concern in Rails

Given that I have a Personable concern in my Rails 4 application which has a full_name method, how would I go about testing this using RSpec? concerns/personable.rb module Personable extend ActiveSupport::Concern def full_name …
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
132
votes
8 answers

All Ruby tests raising: undefined method `authenticate' for nil:NilClass

Most of my tests are raising the following and I don't understand why. All methods call raise the 'authenticate' error. I've checked the code if there was a method called "authenticate" but there is no such method. 1) Admin::CommentsController…
Jeffrey W.
  • 4,169
  • 3
  • 17
  • 18
129
votes
10 answers

Rspec, Rails: how to test private methods of controllers?

I have controller: class AccountController < ApplicationController def index end private def current_account @current_account ||= current_user.account end end How to test private method current_account with rspec? P.S. I use Rspec2…
petRUShka
  • 9,812
  • 12
  • 61
  • 95
128
votes
4 answers

What's the difference between RSpec's subject and let? When should they be used or not?

http://betterspecs.org/#subject has some info about subject and let. However, I am still unclear on the difference between them. Furthermore, the SO post What is the argument against using before, let and subject in RSpec tests? said it is better to…
new2cpp
  • 3,311
  • 5
  • 28
  • 39
128
votes
4 answers

RSpec: Expect to change multiple

I want to check for many changes in a model when submitting a form in a feature spec. For example, I want to make sure that the user name was changed from X to Y, and that the encrypted password was changed by any value. I know there are some…
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
127
votes
4 answers

How to say "should_receive" more times in RSpec

I have this in my test Project.should_receive(:find).with(@project).and_return(@project) but when object receive that method call two times, I have to…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
127
votes
3 answers

RSpec: describe, context, feature, scenario?

describe, context, feature, scenario: What is the difference(s) among the four and when do I use each one?
Mike Glaz
  • 5,352
  • 8
  • 46
  • 73
126
votes
1 answer

How to find which rspec test is taking so long

One (or a couple) of our tests are taking forever and we'd like to optimize them. We have say 1000 tests so it's impractical for me to go through run each file. Is there an easy to way to find the slow ones? This is rspec 1.3
Brian Armstrong
  • 19,707
  • 17
  • 115
  • 144
125
votes
18 answers

Skip callbacks on Factory Girl and Rspec

I'm testing a model with an after create callback that I'd like to run only on some occasions while testing. How can I skip/run callbacks from a factory? class User < ActiveRecord::Base after_create :run_something …
luizbranco
  • 2,841
  • 4
  • 18
  • 22