Rails shoulda and factory_girl setup - ruby-on-rails

I have installed both shoulda and factory_girl, I can run shoulda just fine, but when I add this:
require 'factory_girl'
Factory.define :user do |u|
u.mail 'test#example.com'
u.pass 'secret'
end
to my test/test_helper.rb I'm getting this error:
/test/test_helper.rb:1:in `require': no such file to load -- factory_girl (LoadError)
when I execute rake test:units
I have installed both gems using:
sudo gem install thoughtbot-shoulda --source=http://gems.github.com
sudo gem install thoughtbot-factory_girl --source=http://gems.github.com
and can see both of them being installed fine.
And by the way, this works fine as well:
script/console
Loading development environment (Rails 2.3.8)
require 'factory_girl'
=> []
so requiring the gems seems to be working

Couple things to check:
Don't use --source=http://gems.github.com anymore. This has been deprecated in favor for gemcutter.org
Don't use "thoughtbot-gem_name" - again this was deprecated by gemcutter.org. Just do sudo gem install factory_girl shoulda
Make sure you have config.gem 'factory_girl' in your config/environments/test.rb file.

if you are using spork, make sure to restart it. Took me a few moments to realize that.

Related

Getting started with rspec

I'm trying to do the labs on TestFirst.org with rspec. I've installed rspec and initilized it in the project directory. But when i "rake" i get this error message:
Could not find 'rspec' <~> 2) - did find: [rspec-3.0.0]
C:/Sites/RubyTest/RubyTesting/learn_ruby/rakefile:2:in `<top (required)>ยด
My versions:
ruby 1.9.3p545
rails 4.1.1
rspec 3.0.2
Seems like I've got the wrong version of rspec or something. My OS is windows 7 btw.
This is the content of rakefile:
gem 'rspec', '~>2'
require 'rspec/core/rake_task'
task :default => :spec
desc "run tests for this lab"
RSpec::Core::RakeTask.new do |task|
lab = Rake.application.original_dir
task.pattern = "#{lab}/*_spec.rb"
task.rspec_opts = [ "-I#{lab}", "-I#{lab}/solution", '-f documentation', '-r ./rspec_config']
task.verbose = false
end
This is because rspec ~3.0.0 is already activated and your rake file requires 'rspec', '~>2'. Change rspec version in Rakefile to fix this.
gem 'rspec', '~>3'
Looks like you have a installed version of rspec (3.0.2) that is more recent than the one expected by the project (~ 2.x).
One simple way to solve it would be to force bundle to use the project version:
bundle exec rake
(instead of just using rake)

Using ActiveRecord 3.1 without Rails in a script

The following ought to work as a script, with ActiveRecord 3.1 (note that this is without Rails, not the other way around):
#!/usr/bin/env ruby
require "rubygems"
require "active_record"
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
irb
Unfortunately, it gives the error:
... connection_specification.rb:71:in `rescue in establish_connection': ...
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` ...
Adding the line gem 'mysql2', '<0.3' before require "active_record" as suggested by some previous posts (which reference 0.2.7, the 0.2 gem at the time; presently it's 0.2.18) doesn't change it.
How can I get it to work? I want ActiveRecord but not the whole of Rails.
Run in terminal
gem install mysql2
and add row to you code require 'mysql2'
#!/usr/bin/env ruby
require "rubygems"
require 'mysql2'
require "active_record"
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
This has fixed my issue, at least temporarily. I haven't yet restarted, so I don't know if it'll survive that. And it seems like a dirty hack; there's got to be a better way.
I haven't yet tested Aleksei's answer above, since I'm not having the same issue after having run this command. Will update ifwhen I do.
sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib ~/.rvm/gems/`rvm current`/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

Rails - rake:gems:install - not installing gems

If i define a few gems in my config/environments/test.rb file like this:
config.gem "rspec"
config.gem "rspec-rails"
config.gem "mocha"
and then run 'rake gems:install RAILS_ENV=test'
I get the following error:
Missing these required gems:
mocha
Run `rake gems:install` to install the missing gems.
however if I run rake gems:install like it says it will continue to recurse like this forever.
How do I actually get the gems to install using rake (not gem install)?
thanks!
I wonder, is there a reference to something from the mocha gem in your rake file or environment.rb file? I've seen issues like this before and it presents as this type of problem.
Try installing mocha 'manually' with...
gem install mocha
Then see if you can run rake gems:install.
I ran into this problem as well, and followed the directions here to resolve it. Specifically, deleting and regenerating lib/tasks/rspec.rake is pretty crucial. Also, adding
:lib => false
to
config.gem "rspec", :lib => false, :version => ">= 1.2.0"
helped.
I found this was a GEM_PATH issue. Basically, rails can't find the gems you've installed and even though they're there, they're completely invisible. A bit weird, but hey.
On dreamhost I had to configure the line:
ENV['GEM_PATH'] = '/home/<my_account>/.gems:/usr/lib/ruby/gems/1.8/gems'
in config/environment.rb
but on my dev box this doesn't work for me and has to be removed entirely.
YMMV but I'd suggest that's a good place to start looking.
Which platform are you using? If it it window then
Execute Below command and its works
gem install mocha --platform=mswin32

Why is autotest not working?

I changed my .autotest file to use it with a Ruby-based project.
After that, when I wanted to use it for Rails, it is using the .autotest configuration settings I used for the Ruby project.
I uninstalled autotest and reinstalled it with no luck.
I also removed the .autotest file in the root directory but it is not working.
I'm trying to get autotest up and running as well. I just installed the gem. Running autotest or autotest --rails inside my rails app starts autotest, but it doesn't runs a single test. It reports that there aren't any.
UPDATE:
Just discovered I needed to install autotest-rails as well.
You should also install autotest-fsevent to make sure that autotest isn't polling all the time.
I've posted the results of my day of autotest at http://ryanbooker.com/archive/autotest-your-rails-apps.
The Short story:
sudo gem install ZenTest autotest-rails autotest-fsevent autotest-growl redgreen
Edit your ~/.autotest
# Include plugins
require 'autotest/fsevent'
require 'autotest/growl'
require 'redgreen/autotest' # yes this is correct
# Skip some paths
Autotest.add_hook :initialize do |autotest|
%w{.git .DS_Store ._* vendor}.each { |exception| autotest.add_exception(exception) }
false
end
You can launch autotest with:
cd myrailsapp
autotest
how are you launching autotest? If you use autotest --rails it should definitely work.
If you are using Rails 3.1 or higher, I highly recommend using guard for TDD with rspec. It works like magic. https://github.com/guard/guard

Couldn't find 'rspec' generator

I'm trying to install RSpec as a gem after having it installed as a plugin. I've gone ahead and followed the directions found here http://github.com/dchelimsky/rspec-rails/wikis for the section titled rspec and rspec-rails gems. When I run ruby script/generate rspec, I get the error Couldn't find 'rspec' generator. Do only the plugins work? If so, why do they even offer the gems for rspec and rspec-rails? I'm running a frozen copy of Rails 2.1.2, and the version of rpsec and rspec-rails I'm using is the newest for today (Nov 7, 2008) 1.1.11.
EDIT Nov 12, 2008
I have both the rspec and rspec-rails gems installed. I've unpacked the gems into the vender/gems folder. Both are version 1.1.11.
Since RSpec has been become the default testing framework in Rails you no longer need to create spec docs via the rspec generators:
Rails 2 RSpec generator
rails generate rspec_model mymodel
Rails 3 RSpec generator
With RSpec as the default testing framework simply use Rails' own generators. This will construct all of the files you need including the RSpec tests. e.g.
$rails generate model mymodel
invoke active_record
create db/migrate/20110531144454_create_mymodels.rb
create app/models/mymodel.rb
invoke rspec
create spec/models/mymodel_spec.rb
Have you installed both rspec and rspec-rails gems?
script/generate rspec
requires rspec-rails gem to be installed.
For Rails 3 and rspec 2+
You must make sure you include 'rspec' and rspec-rails' in your Gemfile
Run Bundle Install
then run rails g rspec:install
If you are using rails 2.3 You need to use
ruby script/plugin install git://github.com/dchelimsky/rspec-rails.git -r 'refs/tags/1.3.3'
and then
ruby script/generate rspec
Is there supposed to be an 'rspec' generator? I've only used the following:
script/generate rspec_model mymodel
script/generate rspec_controller mycontroller
I've had this problem before, it boiled down to the version of RSpec I had not working with the version of Rails I was using. IIRC it was a 2.1 Rails and the updated RSpec hadn't been released as a gem. In fact, 1.1.11 is the gem I have, which would be the latest available (ignoring github gems), so I'm pretty sure that's exactly what my problem was.
I've taken to just using the head of master rspec with whatever version of Rails I happen to be on, it seems stable to me (and isn't going to break things in production, unless somehow a test broke with a false positive).
I do it with git using submodules, for example:
git submodule add git://github.com/dchelimsky/rspec.git vendor/plugins/rspec
git submodule add git://github.com/dchelimsky/rspec-rails.git vendor/plugins/rspec_on_rails
In case anyone is wondering about Rails 3 now,
this seems to do the trick for me:
http://github.com/rspec/rspec-rails/blob/29817932b99fc45adaa93c3f75d503c69aafcaef/README.markdown
I'm using rails 2.3.9. I started of trying to use the gem(s) but just couldn't get the generator for rspec to show up. Then I installed the plugin(s) using the instructions on https://github.com/dchelimsky/rspec/wiki/rails and that did the trick.
On Fedora 9 (OLPC) I did:
$ sudo gem install rspec
$ sudo gem install rspec-rails
Those got me to where I could run
$ ruby script/generate rspec
This worked for me, whereas the git instructions did not work.
If you are using bundler version 1.0.8 you should $ gem update bundler to a newer version 1.0.9.
I had the same symptons and updating bundler helped me out.
Now $ rails g is using gems defined in the Gemfile. Also I grouped my gems like this:
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', :require => 'sqlite3'
group :test, :development do
gem 'capybara', '0.4.1.1'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'rspec-rails', '~> 2.4'
gem 'launchy'
end
(Note that test gems are also in the :development group.)
Have a nice day :)
Lukas
If you type script/rails generate, the only RSpec generator you'll actually see is rspec:install. That's because RSpec is registered with Rails as the test framework, so whenever you generate application components like models, controllers, etc, RSpec specs are generated instead of Test::Unit tests.
Please note that the generators are there to help you get started, but they are no substitute for writing your own examples, and they are only guaranteed to work out of the box for the default scenario (ActiveRecord & Webrat).
https://github.com/rspec/rspec-rails
You might need to run bundle exec :
bundle exec rails g rspec:install
You'll need to do
sudo gem install cucumber-rails

Resources