Second question on using RSpec for a simple Rails 2.0 app

Hi,

I’ve got a Rails 2.0 app “AIMS_1” with one model, “Portfolio”. (I’m following David Chelimsky’s 2007 tutorial at http://blog.davidchelimsky.net/articles/2007/05/14/an-introduction-to-rspec-part-i). Portfolio has two attributes: :symbol and :name. With great help from this NG, I’ve got a neat set of validations for :symbol.

I want to put together a script that will test the validations as part of a suite of tests to confirm the app’s validity as I expand it. For this purpose, I added a Q&A directory (actually, “__QA”) under AIMS_1 and populated it with the following portfolio_spec.rb file:

# portfolio_spec.rb # K:\_Projects\Ruby\_Rails_Apps\AIMS_1\__QA

require 'rubygems' require 'spec' require 'spec/story' require 'K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/gems/1.8/gems/ activerecord-2.0.2/lib/ActiveRecord' require '../app/models/portfolio'

describe Portfolio do   it "should report any invalid :stock entries" do     pf = Portfolio.new   end end

When I run “spec portfolio_spec.rb” in a Command window I get the diagnostic:

ActiveRecord::ConnectionNotEstablished in 'Portfolio should report any invalid :stock entries'.

I expected some error because I’m creating a Portfolio instance outside the AIMS_1 framework generated by Rails. I have no idea how to address this issue.

I’m running ruby 1.8.6, Rails 2.0.2 and SciTE 1.74 over WinXP/SP2.

Any ideas?

Thanks in Advance, Richard

RichardOnRails wrote:

When I run spec portfolio_spec.rb in a Command window I get the diagnostic:

ActiveRecord::ConnectionNotEstablished in 'Portfolio should report any invalid :stock entries'.

I expected some error because I'm creating a Portfolio instance outside the AIMS_1 framework generated by Rails. I have no idea how to address this issue.

I'm running ruby 1.8.6, Rails 2.0.2 and SciTE 1.74 over WinXP/SP2.

Any ideas?

Thanks in Advance, Richard

Are you using the Rspec-Rails plugin? Cos that should absorb a lot of this capacity for error. (looks like you just want to require "../../config/environment" or something to fix it, so you load rails, rather than just active_record)

but...

install it as a plugin (maybe install rspec as a plugin in the same way else it seems to choke if your rspec and rspec-rails versions are different)

then run "ruby script/generate rspec" (don't ask me why this isnt a rake task "spec:setup".. but it isn't)

and you should have a basic spec_helper in place.

then try creating a new model

"ruby script/generate rspec_model Furbie name:string age:integer"

and it should get you a new spec in spec/models/furbie_spec.rb which should work, and then make your existing code work the same way... see what went wrong the first time...

Regards Matthew Rudy

(recommend me on WorkingWithRails http://workingwithrails.com/person/12394-matthew-rudy-jacobs)

Hi Matthew,

Thank you very much for your excellent help.

First the good news: Your suggestion about requiring the configuration was spot on! So I’m a happy camper and will indeed make the post you requested at the end of your response.

While I plan to continue using the rspec gem for this current project, I’m very interested in using the two plug-ins you recommended on future projects. However, I wasn’t able to get rspec plug-in to install. Below are the steps I took to: 1. Get the rspec-rails plug-in installed 2. When that failed, try a different plug-in to show plug-in installation is working, generally.

I hope you can decipher my current error. Your unraveling my last error was neat!

Best wishes, Richard

[snip]\AIMS_1>gem uninstall rspec [OK]

[snip]\AIMS_1>ruby script/plugin discover [OK, but showed no dchelimsky]

[snip]\AIMS_1>ruby script/plugin source http://github.com/dchelimsky/ [OK]

[snip]\AIMS_1>ruby script/plugin install rspec-rails Plugin not found: ["rspec-rails"]

[snip]\AIMS_1>ruby script/plugin sources http://dev.rubyonrails.com/svn/rails/plugins/ http://github.com/dchelimsky/ http://svn.6brand.com/projects/plugins/

[snip]\ AIMS_1>ruby script/plugin install simple_pages + ./CHANGELOG + ./README + ./Rakefile + ./app/controllers/simple_pages_controller.rb + ./app/helpers/simple_pages_helper.rb + ./app/models/simple_page.rb [snip]

Check out http://github.com/dchelimsky/rspec-rails/wikis.

Cheers, David

Hi David,

Thanks for that source. I'll try your plug-ins after the Fourth.

Best wishes, Richard