Can you use RSpec, Shoulda, RCov? - ruby-on-rails-3

I am attempting to get RCov to work with my RSpec and Shoulda test for a rails 3 app. It seems to work fine with my RSpec after using the Rake task below but all of the shoulda tests fail and I cant seem to find any documentation on getting these to work. They all run fine under autotest(rspec and shoulda).
namespace :spec do
desc "Run specs with RCov"
RSpec::Core::RakeTask.new('rcov' ) do |t|
#t.spec_files = FileList['spec/**/*_spec.rb' ]
t.rcov = true
t.rcov_opts = ['--exclude' , '\/Library\/Ruby' ]
end
end

What version of Ruby are you using?
I've found that Rcov doesn't work so well with Ruby 1.9, although last I checked was about 2 months ago.
I've switched over to cover_me, which is built on top of Rcov (I believe), and was built for Ruby 1.9.
I haven't had any problems with it so far, and it's easily installed.
Haven't tried it with Shoulda, but works great in Ruby 1.9, Rails 3, and Rspec 2.
Here's the link if you're interested: https://github.com/markbates/cover_me

Related

Capybara Webkit Capybara::Webkit::ConnectionError failed to start

I'm using capybara webkit on Ubuntu (14.04 LTS) and I'm getting the following error when trying to use it:
Capybara::Webkit::ConnectionError: /srv/www/app/shared/bundle/ruby/2.1.0/gems/capybara-webkit-1.3.1/bin/webkit_server failed to start.
from /srv/www/app/shared/bundle/ruby/2.1.0/gems/capybara-webkit-1.3.1/lib/capybara/webkit/connection.rb:75:in `parse_port'
from /srv/www/app/shared/bundle/ruby/2.1.0/gems/capybara-webkit-1.3.1/lib/capybara/webkit/connection.rb:81:in `discover_port'
from /srv/www/app/shared/bundle/ruby/2.1.0/gems/capybara-webkit-1.3.1/lib/capybara/webkit/connection.rb:62:in `start_server'
from /srv/www/app/shared/bundle/ruby/2.1.0/gems/capybara-webkit-1.3.1/lib/capybara/webkit/connection.rb:25:in `initialize'
from /srv/www/app/shared/bundle/ruby/2.1.0/gems/capybara-webkit-1.3.1/lib/capybara/webkit/driver.rb:17:in `new'
I installed QT using:
sudo apt-get install libqt4-dev libqtwebkit-dev libqt5webkit5-dev
Using gem versions: capybara (2.4.4) and capybara-webkit (1.3.1)
The same program works fine on mac (qt installed using homebrew)
Thanks
It has been a long time since this question was asked, but I had the same problem even though I used much more ancient versions of anything. It turned out that webkit needs to be able to connect to some X-Server and this is its reaction if it fails. I ended up installing xvfb and using
xvfb-run --auto-servernum bundle exec rake test
(aliased of course) when running my tests. This is probably less than optimal, but it was good enough for me. Maybe this helps the next person who stumbles across this error.
I beat my head against this all morning. Turns out I had omitted this code from rails_helper.rb :
if ENV['HEADLESS']
require 'headless'
headless = Headless.new
headless.start
at_exit { headless.stop }
end
We use the HEADLESS environment variable to trigger this. Not sure if that's typical or a local convention. Regardless, I needed to add export HEADLESS=1 to .env to fire that off.
I also had to add gem 'headless', '~> 1.0.2' in Gemfile.

rails 3.1 scaffold generator broke for project, just stopped working: no such file to load -- rails_generator (LoadError)

SOLVED, see comment below, discovered 'enumerated_attribute' gem breaks rails scaffold
My rails 3.1 scaffolding just broke for my project. (scaffolding still works fine in my other 3.0 and 3.1 projects-- only one project has a broken scaffold generator!)
In my rail 3.1 project, I have scaffolded multiple models the past few days, then today out of the blue rails g scaffold Mymodel is failing with the error message:
/Users/johndoe/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in
`require': no such file to load -- rails_generator (LoadError)
I didn't switch rails versions mid-way or anything, the project has always been rails 3.1.3, and it's been working fine until I tried it again today for a new model.
Does anyone have any idea how to fix it and/or what might have caused it to suddenly break?
(rails g migration works fine... it's just scaffolding that is broken!)
Solved. Here is the fix:
Comment out enumerated_attribute gem in the Gemfile
Do scaffolding
Replace enumerated_attribute gem in the Gemfile.
Just removing enumerated_attribute did not work for me. I then got errors complaining about undefined method 'enum_attr' (as you would expect), meaning I need to also comment out every attr of my models using enum_attr. I used the following gem version:
gem 'enumerated_attribute', :git => 'git://github.com/jeffp/enumerated_attribute.git'
This fixed the issue for me.

Rails 3 console always starts in test environment

My rails 3 app always is stuck in the Test environment. When I call
rake db:reset
it resets the test database, but not the development one.
When I run the following code, it loads the test environment in the console:
rails c
Trying to specify the development environment also does not work:
jon#jon-laptop:~/id$ RAILS_ENV=development rails console
Loading test environment (Rails 3.0.8)
ruby-1.8.7-p334 :001 >
Starting the server does work normally:
rails s
This is very annoying. Any ideas on where I should look to resolve this?
Thanks in advance,
EDIT
I also tried going back in history to earlier commit to before the problem existed (I think) and it does not fix the problem...
The easiest way is to set the environment is probably by using the RAILS_ENV environment variable, e.g.:
RAILS_ENV=test rails console
Edit: Which version of Rails are you using? This works fine for me on 3.0.7:
rails c [environment]
E.g.
rails c development
I found the problem. I had the following lines in one of my initializers:
ActionMailer::Base.default_url_options[:host] = "localhost:3000" if Rails.env == "development"
ActionMailer::Base.default_url_options[:host] = "localhost:3000" if Rails.env = "test"
Can you spot the mistake???

no such file to load -- action_controller/integration - NoMethodError for Rails 3 and Webrat

I'm getting the following failure during RSpec tests..
no such file to load -- action_controller/integration
..using Rails 3, RSpec 2 and Webrat, if I include the Webrat helpers in the following way (the idea was to use Webrat for the have_tag and have_selector methods instead of assert_select).
RSpec.configure do |config|
..
config.include Webrat::HaveTagMatcher
end
Yet apparently Webrat and Rails 3 are not compatible yet. One solution is to avoid the Webrat gem and to use assert_select instead. Has anyone a better solution? How do you avoid the error?
I have not found a solution for this problem, only a work around. You can use the have_selector method of Capybara instead of Webrat, at least in RSpec request tests. There are certain difficulties with RSpec2 and Capybara (page.should have_selector only works if you use Capybara's 'visit' method, and not the RSpec method get '/some/path'), but basically it works.

Create a rails 3 plugin with RSpec

The question explains itself...
I wanna test a new rails plugin with rspec, are there any specific generators, gems?
I used rails g plugin blabla and I got an error on rspec generator: error rspec [not found].
FYI - there is an open github issue about this: https://github.com/rspec/rspec-rails/issues/#issue/258.