rspec

RSpec vs. Shoulda?

I am new to the unit testing seen; I have only been using unit tests for about 2 months now. When I unit test in Ruby I currently follow the TDD style and use Test::Unit::TestCase. I have also read about RSpec and how it follows the BDD methodology. I also have read about Shoulda which is a in between the two frameworks. My question is, ...

Adding custom directories (source and spec) to autotest in a Rails 3 project

I have a Rails 3 app that uses RSpec2 as my testing framework, and I'm able to use autotest to watch my model and spec directories for changes and re-run my spec suite when files change. I'd like to add a directory with some custom classes in it (RAILS_ROOT/lib/some_project/lib/*.rb) and their corresponding specs (RAILS_ROOT/spec/some_p...

RoR, RSpec - problem with validation testing

Through rspec (I'm using rspec-1.3.0, rspec-rails-1.3.2 gems) generator (ruby script/generate rspec_model suggestion section_id:integer user_id:integer subject:string content:text state:string type:string) I created model and model spec and run rake db:migrate and rake:test:prepare After that I started to work on my model spec: require...

rspec asserting encrypted password not plain text

How would I write a test in rspec to ensure passwords are not stored as plain text in my Ruby on Rails application? I don't really care about the implementation yet as I don't know exactly what plugin or how I am going to write the password encryption (there are lots of examples). I just want a failing test that's independent of the ...

Rspec: How to test recursion?

I'd like to test that a method is called recursively with a specific argument. My approach: class Recursable def rec(arg) rec(7) unless arg == 7 end end describe Recursable do it "should recurse" do r = Recursable.new('test') r.should_receive(:rec).with(0).ordered r.should_receive(:rec).with(7).ordered r.rec(...

RSpec Mock Object Example

I am new to mock objects, and I am trying to learn how to use them in RSpec. Can someone please post an example (a hello RSpec Mock object world type example), or a link (or any other reference) on how to use the RSpec mock object api? ...

Rails3 RSpec is wiping wrong database?

I am trying to get my hands on Rspec2 with Rails3 (never used rspec before). I have rspec-rails 2.0.0.beta20. After introducing some basic tests into spec/models and running rspec spec/models/user_spec.rb everthing is fine. However if I just run rake spec My development database is beeing wiped out. Even if I specify the environme...

Anyone know why I'm getting this RSpec error? getting nil <--

def mock_category(stubs={}) @mock_category ||= mock_model(Category, stubs).as_null_object end describe "GET show" do it "assigns the requested category as @category" do Category.stub(:find).with("37") { mock_category } get :show, :id => "37" assigns(:category).should be(mock_category) end end Which returns : 1) Cate...

rake task on gem

I have a rake task for a series of rspecs as follows... require 'spec/rake/spectask' require 'joliscrapper' namespace :spec do desc "Web scraping files" task :scrapers => :environment do Spec::Rake::SpecTask.new do |t| t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] t.spec_files = FileList['spec/scr...

What not to test in Rails?

I've been writing tests for a while now and I'm starting to get the hang of things. But I've got some questions concerning how much test coverage is really necessary. The consensus seems pretty clear: more coverage is always better. But, from a beginner's perspective at least, I wonder if this is really true. Take this totally vanilla c...

Accidentally uninstall rspec..

Trying to reinstall RSpec, and I can't seem to re-add its command to my bin folder. Mac-Users-MacBook-Pro:bin macuser$ rspec -bash: /usr/bin/rspec: No such file or directory >> which rspec #> returns nothing. I tried sudo gem install rspec --prerelease a dozen times, bundle install , and nothing seems to give. What am I missing? ...

should i only be testing public interfaces in BDD (in general, and specifically in ruby)

I'm reading through the (still beta) rspec book by the prag progs as I'm interested in behavioral testing on objects. From what I've gleaned so far (caveat: after only reading for 30 min) the basic idea is that I want ensure my object behaves as expected 'externally' ie. in its output and in relation to other objects. Is it true then t...

Cucumber/Webrat not following the redirect_to

I am running rails 3.0.0, rspec-rails 2.0.0.beta.20, webrat 0.7.2.beta.1, cucumber-rails 0.3.2 I have this scenario: Scenario: Given I am on the new account page And I fill in "Name" with "John Doe" When I press "Create" Then I should be on the access page When I run it I get: expected: "/access", got: "/accounts" Like its ...

individual spec passes when run alone, but fails when all specs are run

I have 30 specs in my foo_controller_spec.rb and when I run the entire file using spec, I get 4 failures and 2 pending. When I run the 4 failing specs individually, 3 of them still fail, but one of them passes. At first I thought it was a database issue, that the data wasn't being cleaned out properly between runs. So I installed databas...

Can someone describe to me what RSpec 2 is doing in this?

I have been attempting to dive into RSpec 2 but its auto generated controller specs do not work for any version of RSpec 2 with any version of Ruby or any version of Rails. Maybe I'm missing something obvious? def mock_category(stubs={}) @mock_category ||= mock_model(Category, stubs).as_null_object end describe "GET show" do it "as...

How to completely remove Rspec and its generated files, and reinstall them into a project?

Is it possible to completely remove rspec from a project, and then reinstall it again? The reason I ask is because I had started my project on Rails 3 beta, and now that the new one has come out, the new rspec conflicts and nothing passes. But if I start a new project, everything seems to work as expected. ...

Rails 3 with Rspec2 and authlogic: Cannot spec requests & views

I'm having trouble creating specs for my views and requests. Some of my controllers use named_scope, like this: #projects_controller.rb @projects = Project.with_user( current_user) ## project.rb: scope :with_user, lambda {|u| {:joins => :client, :conditions => {:clients => {:user_id => u.id} } }} but the following spec gives an ...

Can optparse skip unknown options, to be processed later in a ruby program?

Is there any way to kick off optparse several times in one Ruby program, each with different sets of options? Example: $ myscript.rb --subsys1opt a --subsys2opt b here, myscript.rb would use subsys1 and subsys2, delegating their options handling logic to them, possibly in a sequence where 'a' is processed first, followed by 'b' in s...

How do I avoid the "Useless use of == in void context" in RSpec?

In RSpec, if I have warnings on and have x.should == 42 another_line_of_code then I get a warning about warning: useless use of == in void context Is there anything I can do other than Turn warnings off Change it to bitbucket = (x.should == 42) ...

Validating boolean value in Rspec and Rails

I'm pretty confused how to validate boolean values in Rspec and Rails. I understand everything except for false and nil are taken as true in Ruby. But when I use MySQL with Rails, it uses 1 for true and 0 for false (if my understanding is correct). I have the following model spec. I'd like to test boolean value for superuser attribute. ...