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

RSpec: What is the difference between let and a before block?

What is difference between let and a before block in RSpec? And when to use each? What will be good approach (let or before) in below example? let(:user) { User.make !} let(:account) {user.account.make!} before(:each) do @user = User.make! …
kriysna
  • 6,118
  • 7
  • 30
  • 30
96
votes
6 answers

How do I prepare test database(s) for Rails rspec tests without running rake spec?

After significant troubleshooting, I figured out that I needed to run rake spec once (I can abort with control-c) before I can run rspec directly (e.g. on a subset of our specs). We are running Rails 3.0.7 and RSpec 2.5.0. Clearly, rake is running…
gerry3
  • 21,420
  • 9
  • 66
  • 74
96
votes
7 answers

Speeding up RSpec tests in a large Rails application

I have a Rails application with over 2,000 examples in my RSpec tests. Needless to say, it's a large application and there's a lot to be tested. Running these tests at this point is very inefficient and because it takes so long, we're almost at the…
randombits
  • 47,058
  • 76
  • 251
  • 433
95
votes
1 answer

Difference between an it block and a specify block in RSpec

What is the difference between an it block and a specify block in RSpec? subject { MovieList.add_new(10) } specify { subject.should have(10).items } it { subject.track_number.should == 10} They seem to do the same job. Just checking to be sure.
basheps
  • 10,034
  • 11
  • 36
  • 45
95
votes
7 answers

undefined method `visit' when using RSpec and Capybara in rails

I can't get capybara working with rspec. It gives me this error: undefined method `visit' for # I know there are lots of posts about this but non of the solutions are working for me. Most…
lightswitch05
  • 9,058
  • 7
  • 52
  • 75
93
votes
6 answers

How to assert number of elements using Capybara with proper error message?

I know that in Capybara, you can do something like this: page.should have_css("ol li", :count => 2) However, assuming that page has for instance only one matching element, the error is not very descriptive: 1) initial page load shows greetings …
merryprankster
  • 3,369
  • 2
  • 24
  • 26
93
votes
4 answers

Capybara: How do I fill in a input field by its ID

I have this:
How do I fill in a that field by…
Nerian
  • 15,901
  • 13
  • 66
  • 96
93
votes
10 answers

Generate a controller with all the RESTful functions

I am trying to generate a controller with all the RESTful actions stubbed. I had read at Wikibooks - Ruby on Rails that all I needed to do was to call the generator with the controller name and I would get just that. So, I ran script/generate…
Barb
  • 957
  • 1
  • 6
  • 5
92
votes
5 answers

Rspec 3 how to test flash messages

I want to test controller's action and flash messages presence with rspec. action: def create user = Users::User.find_by_email(params[:email]) if user user.send_reset_password_instructions flash[:success] = "Reset password instructions…
Mike Andrianov
  • 3,033
  • 3
  • 23
  • 24
92
votes
4 answers

rails rspec before all vs before each

contest_entry_spec.rb require 'spec_helper' describe ContestEntry do before(:all) do @admission=Factory(:project_admission) @project=Factory(:project_started, :project_type => @admission.project_type) …
Johnny Cash
  • 4,867
  • 5
  • 21
  • 37
91
votes
10 answers

capybara assert attributes of an element

I'm using RSpec2 and Capybara for acceptance testing. I would like to assert that link is disabled or not in Capybara. How can I do this?
kriysna
  • 6,118
  • 7
  • 30
  • 30
90
votes
14 answers

How to test ActionMailer deliver_later with rspec

trying to upgrade to Rails 4.2, using delayed_job_active_record. I've not set the delayed_job backend for test environment as thought that way jobs would execute straight away. I'm trying to test the new 'deliver_later' method with RSpec, but I'm…
bobomoreno
  • 2,848
  • 5
  • 23
  • 42
89
votes
5 answers

Stubbing authentication in request spec

When writing a request spec, how do you set sessions and/or stub controller methods? I'm trying to stub out authentication in my integration tests - rspec/requests Here's an example of a test require File.dirname(__FILE__) +…
Jonas Bylov
  • 1,494
  • 2
  • 15
  • 28
88
votes
3 answers

How to create a Gemfile?

I'm very new to Ruby. I was following a blog post that says that in order to install a required dependencies I need to create a Gemfile. How do I create a Gemfile with rspec as a dependency?
Saurabh Juneja
  • 1,187
  • 1
  • 8
  • 12
87
votes
1 answer

Ruby Rspec : Testing instance variables without adding an accessor to source

I'm trying to test the following method: def unprocess_move(board, move) if move[0].instance_of?(Array) multi_move = @multi_move.pop(2).reverse multi_move.each do |single_move| unapply_move(board, single_move) end else …
steve_gallagher
  • 3,778
  • 8
  • 33
  • 51