rspec

Rake stats and Cucumber

Hello, I'm using Cucumber with RSpec in a Rails project. When I use the "rake stats" task, I get the following : +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+...

Can someone show me an example of using failure_message_for_should using rspec?

I am trying to return more informative failure messages from an expectation. I have a helper method that iterates a hash, testing for mass assignment protection on a model. If the expectation fails I would like to output which attribute failed. I have come across failure_message_for_should, but I am not sure how to properly use it. Tha...

Stubbing named_scope in an RSpec Controller

I haven't been able to find anything for a situation like this. I have a model which has a named scope defined thusly: class Customer < ActiveRecord::Base # ... named_scope :active_customers, :conditions => { :active => true } end and I'm trying to stub it out in my Controller spec: # spec/customers_controller_spec.rb describe C...

How can I spec out an authlogic sessions controller using using a stub?

I want to test my User Session Controller testing that a user session is first built then saved. My UserSession class looks like this: class UserSession < Authlogic::Session::Base end The create method of my UserSessionsController looks like this: def create @user_session = UserSession.new(params[:user_session]) if @user_sess...

Testing ApplicationHelper methods that rely on the current controller path

I have an method in ApplicationHelper def javascript_auto_link_tags js_path = "#{RAILS_ROOT}/public/javascripts" js = "#{controller.controller_path}.js" javascript_include_tag(js) if FileTest.exist?("#{js_path}/#{js}") end This should find a javascript file based on the current controller file name and path so controllers/admin/...

Variable Passing in Rails Controller Tests

I am doing some controller testing with RSpec and Mocha. Here is an example describe MenuItemsController, "creating a new menu item" do integrate_views fixtures :menu_items it "should redirect to index with a notice on successful save" do MenuItem.any_instance.stubs(:valid?).returns(true) post 'create' assigns[:menu_i...

How can I use more than one set of fixtures per table in Rails?

I want to define alternative sets of fixtures per database table. For example, class BusinessSpec describe "some aspect of businesses" do fixtures :fixture_set_1 ... end describe "some unrelated aspect of businesses" do fixtures :fixture_set_2 ... end end Is this, or something similar, possible? ...

Adding to RSpec's search path

I'm using a mocking framework add-on to RSpec called NotAMock (http://github.com/notahat/not%5Fa%5Fmock). I am modeling a business layer in Ruby, so my project consists of mostly plain-old Ruby objects (PORO's). I've put my specs in a 'specs' directory off of the project directory, and put NotAMock within the specs directory. When I r...

Can I use rspec mocks to stub out version constants?

I've got code that only needs to run on a certain version of ActiveRecord (a workaround for a bug on old AR libraries). This code tests the values of ActiveRecord::VERSION constants to see if it needs to be run. Is there a way to mock out those constants in rspec so I can test that code path without relying on having the right ActiveRec...

TDD/BDD Rails Cucumber / RSpec duplication

Can someone please clarify using a SIMPLE user story the full slice of what Cucumber would be used for and what RSpec would be used for? I purchased the RSpec book the other day and have been going through it. The author seems to be quite vague at times. What I'm thinking of if the user story is something like (please excuse the synta...

Selenium in Ruby - can't get it working, and it's reset firefox to it's default installed state

I've been using Selenium (with selenium rc, and the ide) in ruby, with rspec, for some time and it's been great. I recently wiped my installation of ubuntu, however, and installed ubuntu 9.10. I set selenium up again, which consisted of installing the selenium-client gem (1.2.17) and adding the selenium ide plugin in firefox. When i r...

How do I stub/mock a call to the command line with rspec?

I'm trying to test output from a command line tool. How do I 'fake' a command line call with rspec? Doing the following doesn't work: it "should call the command line and return 'text'" do @p = Pig.new @p.should_receive(:run).with('my_command_line_tool_call').and_return('result text') end How do I create that stub? ...

Cucumber and/or RSpec with pure Java application using JRuby

After doing some stuff in Ruby on Rails with Cucumber, RSpec and Ruby BDD in general, comming back to JUnit in my Java apps feels like incredible pain to me. I just love the convenience that Ruby brings into testing. From what I understand, it should be theoreticaly possible to use Cucumber features, or even RSpec mocking with pure Java...

Rspec, Model load order, fixtures and named_scope challenge

I have some players and the players have a trade state. Rather than hard code trade states like "active" and "inactive" and then have to go looking for strings, I thought I'd be clever and have a separate TradeState model so that a Player has a trade_state_id (a Player can be in only one trade state at a time). Now, it would be a conve...

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

rspec testing views with internationalization ?

Hello, I want to make sure I have the right meta desc/keyword and title text in my view so I want to create rspec views tests for that. Now the real challange here is how to make it work across multiple languages. The way I've done it is: it "should have the right page title" do title = "some nice title here" response.should have_...

Can't get rspec, spork and debugger to play nice.

Given I am a dumb programmer and I am using rspec and I am using spork and I want to debug ...mmm...let's saaay, a spec for Phone. Then, where should I put the "require 'ruby-debug'" line in order to halt processing at a particular point in the phone_spec.rb? (All I'm asking for is a big fat arrow that even a challenged programme...

How do I get colour with Windows command prompt using RSpec in Ruby?

In other o/s RSpec returns nicely coloured results (red, green etc). However in the windows (Vista) command prompt my text output is just plain old boring white. How can I bring colour to my RSpec test results? Thanks Evolve ...

How to test email sending using Rspec?

What are the best practices and tools to test email-sending using rspec with Rails? For instance, how do I test that an email has been sent or what should I test to have efficient testing and acceptable coverage. If you guys need an example, how would I go and test this: class UserMailer < ActionMailer::Base def jobdesc_has_been_re...

Rspec: undefined method `new' error in ActiveRecord model

I know this has to be something stupid, but I keep getting the following error in one of my examples: undefined method `new' for #<Class:0x211d274> I have created a simple example to show the error: describe LateCharge do before :each do @membership = Membership.new @location = mock_model(Location, :late_payment_rate => 10)...