How to check something before an error is raised

I have the following ruby code class Gateway ... def post begin ... raise ClientError if state == :open rescue ClientError => e Log.add("error") raise end end end On RSpec, how can I check that when ClientError is raised Log.add is called? I

Set custom user agent on rail tests

I am developing iOS app using turbolinks-ios and Rails variant. In my iOS app, I set custom user agent iPadApp and detect that on Rails application controller is using request.user_agent.try(:index, 'iPadApp') for setting variant to tablet (because b

Can not satisfy RSpec

I have this rspec code: let(:valid_attributes) { {name: "Sample Product"} } describe "#index" do it "should give a collection of products" do product = Product.create! valid_attributes get :index, :format => :json expect(r

Rspec - How to create macros with tags?

Is there a way to run Rspec macros conditionally ? For example ability to filter macros using RSpec.configure do |c| c.filter_run_excluding :broken => true end ## This should get skipped it_should_validate_with_macro :some_param, :broken => true Not

How stub in rspec 3?

I have the following code: def when_to_change year, month ="%Y %m").split(' ').map {|v| v.to_i} if month < 9 year + 2 else year + 3 end end And I try to stub it in my spec in the following way: it 'when current month at t

change the event does not fire with poltergeist

I have 2 dropdowns and on change of first am prepopulating the second dropdown(which is been done in backbone). events: -> 'change #client_id': 'selectedClientChange' selectedClientChange: (ev) -> @populateGroups $( W

Switching the Capybara session to a page object

I'm trying to do simple implementation of page object pattern using capybara and rspec. I'm at the point where I need to initialise a new page object from a different page object instance method. But after initialising the new page object the system

How to increase an exception in a rspec test

i'm stuck on a test scenario. i have a controller method: def update @object = Object.find params[:id] # some other stuff rescue ActiveRecord::StaleObjectError # woo other stuff end the first part i test with: context '#update' let(:obje

Confront the matcher argument in RSpec 3

I have a function I'm calling that I need to test the arguments of. Normally I'd do something like: expect(my_obj).to_receive(:my_function).with(include('good_value')) Is there a way to flip this around, though, to be something like expect(my_obj).to

Test and mock the contents of a block with RSpec

I am writing a unit test for one of my service objects. In this particular case, I needed to use transactions to ensure data integrity. Thus, I have a simple code like so: class CreateUser def save user_klass.db.transaction do user = user_klass.creat

Can I be a nested RSpec batons?

I have several RSpec examples that share the following complex expectation, with the array records and the floating point numbers min_long, max_long, min_lat, max_lat varying between these examples. expect(records).to all have_attributes( shape: have

Testing multiple hosts with the same test using serverspec

The Advanced Tips section of the Serverspec site shows an example of testing multiple hosts with the same test set. I've built an example of my own (, but there are problems. The first problem

The RSpec test runs correctly only when run in sequence

I have an issue that seems to be specific to my test suite. I have a module that holds some defaults in a constant like so: module MyModule DEFAULTS = { pool: 15 } def self.options @options ||= DEFAULTS end def self.options=(opts) @options = opts end

Test if a block is passed with RSpec Mocks

I can test if arguments are passed like: RSpec.describe do it do obj = double expect(obj).to receive(:method).with(1, 2, 3) obj.method(1, 2, 3) end end How should I do about a block parameter? My ideal code: RSpec.describe do it do obj = double proc

RSpec model test: failures

I am teaching myself RSpec (v3.1.7). I have installed rspec with rails g rspec:install into an existing rails app - freshly created. I created a model: rails g rspec:model zombie. Ran the migration and all went well. In: app/models/zombie.rb: class Z

Defining the header in the RSpec 3 request

I'm trying to set the header for some RSpec requests that require authentication. The header is ACCESS_TOKEN. No matter how I attempt to set the header, it never gets set. I know the app works because I can manually test it, I just cant get rspec tes

The Rspec test controller is partial to the premises

I have controller action like def get_status status_name = current_user.status status_updated_time = current_user.updated_at render :partial => 'show_status', :locals => {status_name: status_name, status_updated_time: status_updated_time} end here I

Multiple assertions for a single configuration in RSpec

I have a few slower specs that I would like to optimise. The example of such spec looks like: require 'rspec' class HeavyComputation def compute_result sleep 1 # something compute heavy here "very big string" end end describe HeavyComputation, '

Rspec 3.0 belongs_to association

I'm trying to test a belongs_to association using rspec 3.0 but I keep running into the error NoMethodError: undefined method `belong_to' for RSpec::Core it "should belong to a user" do ride = user = user.rides << ride ex

Rspec Controller Tests with Chaining Method Calls

My controller looks like this: def index params[:page]||1 @stories =[:page]).per(5) end When I try to write controller tests for the chaining line through using RSpec, I can't seem to get the tests to pass. My controller_spec lo

What is the Rspec 3.0 matrix for = ~?

Say you have: Article.keys.should =~ [:title, :year, :title_length] Which becomes in Rspec 3.0 expect(Article.keys).to =~ [:title, :year, :title_length] Except that it fails with: ArgumentError: The expect syntax does not support operator matchers, s

waited true to answer true?

I upgraded my rspec-rails to 3.0.1 and now I'm seeing this error on all of my tests Failure/Error: Sidekiq::Status::complete?(json.jid).should be_true expected true to respond to `true?` I can't find the solution nor what I'm missing.From rspec 3.0,