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.
18,245
questions
479
votes
10
answers
144k
views
When to use RSpec let()?
I tend to use before blocks to set instance variables. I then use those variables across my examples. I recently came upon let(). According to RSpec docs, it is used to
... to define a memoized ...
380
votes
16
answers
245k
views
How to run a single RSpec test?
I have the following file:
/spec/controllers/groups_controller_spec.rb
What command in terminal do I use to run just that spec and in what directory do I run the command?
My gem file:
# Test ...
324
votes
15
answers
271k
views
How do you run a single test/spec file in RSpec?
I want to be able to run a single spec file's tests — for the one file I'm editing, for example. rake spec executes all the specs. My project is not a Rails project, so rake spec:doc doesn't ...
280
votes
6
answers
92k
views
Rspec: "array.should == another_array" but without concern for order
I often want to compare arrays and make sure that they contain the same elements, in any order. Is there a concise way to do this in RSpec?
Here are methods that aren't acceptable:
#to_set
For ...
239
votes
17
answers
147k
views
Rails: Missing host to link to! Please provide :host parameter or set default_url_options[:host]
I have been googling for about 90 minutes now and still don't have an answer to this. Where do I set default_url_options? I've already set it for config.action_mailer.default_url_options to solve ...
229
votes
8
answers
46k
views
iOS Tests/Specs TDD/BDD and Integration & Acceptance Testing
What are the best technologies to use for behavior-driven development on the iPhone? And what are some open source example projects that demonstrate sound use of these technologies? Here are some ...
213
votes
5
answers
89k
views
How to use RSpec's should_raise with any kind of exception?
I'd like to do something like this:
some_method.should_raise <any kind of exception, I don't care>
How should I do this?
some_method.should_raise exception
... doesn't work.
188
votes
10
answers
96k
views
How do I run only specific tests in Rspec?
I think there's a way to run only tests with a given label. Anybody know?
186
votes
14
answers
84k
views
Testing modules in RSpec
What are the best practices on testing modules in RSpec? I have some modules that get included in few models and for now I simply have duplicate tests for each model (with few differences). Is there a ...
176
votes
6
answers
90k
views
How can I check that a form field is prefilled correctly using capybara?
I have a field with a proper label that I can fill in with capybara without a problem:
fill_in 'Your name', with: 'John'
I'd like to check the value it has before filling it in and can't figure it ...
169
votes
14
answers
204k
views
How to check for a JSON response using RSpec?
I have the following code in my controller:
format.json { render :json => {
:flashcard => @flashcard,
:lesson => @lesson,
:success => true
}
In my RSpec ...
168
votes
5
answers
49k
views
How can I tell Rails to use RSpec instead of test-unit when creating a new Rails app?
I have test-unit installed and rspec installed (along with -core, -expectations, -mocks and -rails version 2.6.x). When I run the command rails new foo, it uses test-unit to generate the test stub ...
162
votes
4
answers
51k
views
Set up RSpec to test a gem (not Rails)
It is pretty easy with the added generator of rspec-rails to set up RSpec for testing a Rails application. But how about adding RSpec for testing a gem in development?
I am not using jeweler or such ...
160
votes
6
answers
58k
views
How do I globally configure RSpec to keep the '--color' and '--format specdoc' options turned on
How do I set global configuration for RSpec in Ubuntu.
Specifically so, --color and --format specdoc stay turned on, across all my projects (ie every time I run rspec anywhere).
159
votes
8
answers
82k
views
Trouble comparing time with RSpec
I am using Ruby on Rails 4 and the rspec-rails gem 2.14. For a my object I would like to compare the current time with the updated_at object attribute after a controller action run, but I am in ...
155
votes
6
answers
89k
views
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
...
144
votes
4
answers
178k
views
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 ...
143
votes
7
answers
89k
views
How to click first link in list of items after upgrading to Capybara 2.0?
How to click first link in that case:
<div class="item">
<a href="/agree/">Agree</a>
</div>
<div class="item">
<a href="/agree/">Agree</a>
</div>
...
141
votes
15
answers
111k
views
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 ...
140
votes
10
answers
100k
views
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 ...
136
votes
6
answers
31k
views
RSpec vs Cucumber (RSpec stories) [closed]
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 ...
135
votes
4
answers
69k
views
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 ...
134
votes
5
answers
52k
views
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 ...
133
votes
4
answers
117k
views
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::...
133
votes
4
answers
68k
views
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 ...
132
votes
4
answers
85k
views
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 do
Project.should_receive(:find).with(@project)...
132
votes
8
answers
40k
views
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 ...
130
votes
10
answers
64k
views
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 ...
128
votes
1
answer
20k
views
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 ...
127
votes
3
answers
35k
views
RSpec: describe, context, feature, scenario?
describe, context, feature, scenario: What is the difference(s) among the four and when do I use each one?
126
votes
18
answers
66k
views
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
...
122
votes
1
answer
37k
views
What's the difference between RSpec and Cucumber? [closed]
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 ...
119
votes
1
answer
93k
views
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
...
119
votes
1
answer
20k
views
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
...
118
votes
7
answers
57k
views
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 ...
115
votes
8
answers
93k
views
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 `<top (required)>': ...
115
votes
7
answers
103k
views
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:
...
111
votes
4
answers
31k
views
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 ...
104
votes
9
answers
58k
views
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::...
104
votes
6
answers
56k
views
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
...
103
votes
7
answers
95k
views
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 ...
...
101
votes
6
answers
25k
views
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/...
101
votes
2
answers
32k
views
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....
99
votes
12
answers
47k
views
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 ...
99
votes
8
answers
146k
views
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 ...
97
votes
3
answers
75k
views
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?
97
votes
6
answers
128k
views
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 ...
96
votes
6
answers
49k
views
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 ...
96
votes
4
answers
35k
views
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....
96
votes
7
answers
19k
views
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 ...