EzDevInfo.com

rspec

Behaviour Driven Development framework for Ruby RSpec: Behaviour Driven Development for Ruby

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).and_return(@project)
Project.should_receive(:find).with(@project).and_return(@project)

Is there any way how to say something like

Project.should_receive(:find).with(@project).and_return(@project).times(2)

Source: (StackOverflow)

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 and Ruby on Rails 3


Source: (StackOverflow)

Advertisements

Getting Started with RSpec - Looking for tutorials [closed]

I am looking to get started building a project and want to use RSpec from day one. My Ruby background is limited; however, I do have a good understanding of MVC and the structure within Ruby.

In doing some research for books and tutorials, I've found no currently published books, and have found no tutorials that give a good "hello world" type write up. And the documentation on the RSpec site is sparse to say the least. Do any of you have links that you'd like to share of good tutorials of getting started with roR and RSpec?


Source: (StackOverflow)

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.


Source: (StackOverflow)

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 work.

Don't know if this matters, but here is my directory structure.

./Rakefile
./lib
./lib/cushion.rb
./lib/cushion
./lib/cushion/doc.rb
./lib/cushion/db.rb
./spec
./spec/spec.opts
./spec/spec_helper.rb
./spec/db_spec.rb

Source: (StackOverflow)

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 example:

array.to_set.should == another_array.to_set

This fails when the arrays contain duplicate items.

#sort

For example:

array.sort.should == another_array.sort

This fails when the arrays elements don't implement #<=>


Source: (StackOverflow)

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 view on this, is that it's convenient to use Cucumber when you're implementing application for the client and do not understand how the whole system is supposed to work yet.

But what if I'm doing my own project? For most of the time, I know how the parts of the system interact. All I need to do is to write a bunch of unit-tests. What are the possible situations when I would need Cucumber then?

And, as a corresponding second question: do I have to write specs if I write Cucumber stories? Wouldn't it be double-testing of the same thing?


Source: (StackOverflow)

Setup RSpec to test a gem (not Rails)

It is pretty easy with the added generator of rspec-rails to setup RSpec for testing a Rails application. But how about adding RSpec for testing a gem in development? I am not using jeweler or such tools. I just used Bundler (bundle gem my_gem) to setup the structure for the new gem and edit the *.gemspec manually. I also added s.add_development_dependency "rspec", ">= 2.0.0" to gemspec and did a bundle install.

Is there some nice tutorial what to do next to get RSpec working?


Source: (StackOverflow)

rspec how to run a single test?

I have found all kinds of links online but none that are current and show how to run a single 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 dir do I run the command?

My gem file:

# Test ENVIRONMENT GEMS
group :development, :test do
    gem "autotest"
    gem "rspec-rails", "~> 2.4"
    gem "cucumber-rails", ">=0.3.2"
    gem "webrat", ">=0.7.2"
    gem 'factory_girl_rails'
    gem 'email_spec'
end

spec file

require 'spec_helper'

describe GroupsController do
  include Devise::TestHelpers

  describe "GET yourgroups" do
    it "should be successful and return 3 items" do

      Rails.logger.info 'HAIL MARRY'

      get :yourgroups, :format => :json
      response.should be_success
      body = JSON.parse(response.body)
      body.should have(3).items # @user1 has 3 permissions to 3 groups
    end
  end
end

Source: (StackOverflow)

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 way to DRY it up?


Source: (StackOverflow)

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 we can make it pass? Is there is any way to do this?


Source: (StackOverflow)

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 handling GET to index is successful
     Failure/Error: get :index
     undefined method `authenticate!' for nil:NilClass
     # ./spec/controllers/admin/comments_controller_spec.rb:9:in `block (3 levels) in <top (required)>'


  124) PostsController handling GET for a single post should render show template
     Failure/Error: get :show, :year => '2008', :month => '01', :day => '01', :slug => 'a-post'
     undefined method `authenticate' for nil:NilClass
     # ./app/controllers/application_controller.rb:18:in `set_current_user_for_model'
     # ./spec/controllers/posts_controller_spec.rb:131:in `do_get'
     # ./spec/controllers/posts_controller_spec.rb:140:in `block (3 levels) in <top (required)>'

The project can be found over there => https://github.com/agilepandas/enki in case you'd like to run the tests your self.


Source: (StackOverflow)

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 controller test I want to verify that a certain scenario does receive a success json response so I had the following line:

controller.should_receive(:render).with(hash_including(:success => true))

Although when I run my tests I get the following error:

Failure/Error: controller.should_receive(:render).with(hash_including(:success => false))
 (#<AnnoController:0x00000002de0560>).render(hash_including(:success=>false))
     expected: 1 time
     received: 0 times

Am I checking the response incorrectly?


Source: (StackOverflow)

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?


Source: (StackOverflow)

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 time.

Other expectations on the response object, such as response.should render_template('index') pass without any problems.

Have any of you encountered this problem before? Perhaps the response html can be obtained in some other way?

As for versions, Rails 2.1.0, RSpec 1.2.7.


Source: (StackOverflow)