Long post but prolly simple if you understand Rails, RSpec & differences between webrat & capybara. I''m hoping someone can take a few moments to help me get a better understanding of my environment. I have a somewhat intermediate understanding of Ruby/Rails but a beginner''s understanding of RSpec. I have purchased The RSpec Book and, though helpful, leaves me with more questions...probably from the very beginning I missed some basic points...as just understanding HOW the environment is set up, WHERE/ WHY it looks for files to test and WHAT matchers, etc. are available in tests is confusing me. Anyway, in trying to wrap my head around RSpec I created a simple Rails3 project. Altered the Gemfile to: group :development, :test do gem ''rspec-rails'' gem ''webrat'' gem ''haml-rails'' gem ''sass'' gem ''spork'', ''~> 0.9.0.rc'' end group :test do gem ''rspec'' end Generated some scaffolding and starting making and editing some specs... I was happy! Now, after some stability and growing confidence I decided I''d try to swap out webrat and replace it with capybara (which I''ve heard good things about especially when dealing with javascript tests). So I commented out "# gem ''webrat''" and - added: gem ''capybara'', :git => ''git://github.com/jnicklas/capybara.git'' gem ''launchy'' gem ''database_cleaner'' - added/modified what seems like alot of requires to spec_helper.rb (spork, rubygems, rspec/rails, capybara/rspec, capybara/rails, database_cleaner). - changed some of my matchers from, what I think came from, webrat to, what I think works with, Capybara...but I''m getting failures in my controller & view specs. I''d really love to understand WHY and HOW/ WHERE to look so in the future I can diagnose these errors myself. VIEW SPEC: # spec/view/pages/about.html.haml_spec.rb require ''spec_helper'' describe "pages/about.html.haml" do it "should have the right heading" do # render # <- worked with webrat # rendered.should have_selector("h2", :content => "About Us") visit pages_about_path page.should have_selector(''h2'') end end Failures: pages/about.html.haml should have the right heading Failure/Error: visit pages_about_path NoMethodError: undefined method `visit'' for #<RSpec::Core::ExampleGroup::Nested_7:0x0000010320a568> CONTROLLER SPEC: # spec/controllers/pages_controller_spec.rb require ''spec_helper'' describe PagesController do describe "GET ''about''" do it "should be successful" do # get ''about'' # <- worked with webrat # response.should be_success # <- webrat ok visit pages_about_path # visit ''/pages/about.html'' # <- also fails w/capybara page.should have_content(''About Us'') # page.html.should match(/About/i) # no help # page.should match(/About/i) # no help end end Failures: 1) PagesController GET ''about'' should be successful Failure/Error: visit pages_about_path LoadError: no such file to load -- action_controller/integration # ./spec/controllers/pages_controller_spec.rb:10:in `block (3 levels) in <top (required)>''
David Chelimsky
2011-May-26 03:18 UTC
[rspec-users] Newb struggling to get/keep RSpec stable
On May 25, 2011, at 5:56 PM, Meltemi wrote:> group :development, :test do > gem ''rspec-rails'' > gem ''webrat'' > gem ''haml-rails'' > gem ''sass''I''d move haml and sass out of these groups - you need them in production.> gem ''spork'', ''~> 0.9.0.rc'' > end > > group :test do > gem ''rspec'' > endDon''t need ^^ this because rspec-rails already depends on rspec (so it''s already installed).> > Generated some scaffolding and starting making and editing some > specs... I was happy! > > Now, after some stability and growing confidence I decided I''d try to > swap out webrat and replace it with capybara (which I''ve heard good > things about especially when dealing with javascript tests). So I > commented out "# gem ''webrat''" and > - added: > > gem ''capybara'', :git => ''git://github.com/jnicklas/capybara.git''Strongly recommend sticking to releases while you''re getting your feet wet. gem ''capybara'', ''1.0.0.beta1'' Otherwise everytime you update the bundle you''ll get a different revision.> gem ''launchy'' > gem ''database_cleaner'' > > - added/modified what seems like alot of requires to spec_helper.rb > (spork, rubygems, rspec/rails, capybara/rspec, capybara/rails, > database_cleaner). > > - changed some of my matchers from, what I think came from, webrat to, > what I think works with, Capybara...but I''m getting failures in my > controller & view specs. I''d really love to understand WHY and HOW/ > WHERE to look so in the future I can diagnose these errors myself. > > VIEW SPEC: > > # spec/view/pages/about.html.haml_spec.rb > require ''spec_helper'' > > describe "pages/about.html.haml" do > it "should have the right heading" do > # render # <- worked with webrat > # rendered.should have_selector("h2", :content => "About Us") > visit pages_about_path > page.should have_selector(''h2'')visit and page are not available in view specs. Stick with render and rendered. If you''re using Capybara-1.0.0.beta1 you can use the have_selector method in the view spec.> end > end > > Failures: > > pages/about.html.haml should have the right heading > Failure/Error: visit pages_about_path > NoMethodError: > undefined method `visit'' for > #<RSpec::Core::ExampleGroup::Nested_7:0x0000010320a568> > > CONTROLLER SPEC: > > # spec/controllers/pages_controller_spec.rb > require ''spec_helper'' > > describe PagesController do > > describe "GET ''about''" do > it "should be successful" do > # get ''about'' # <- worked with webrat > # response.should be_success # <- webrat ok > visit pages_about_path > # visit ''/pages/about.html'' # <- also fails w/capybara > page.should have_content(''About Us'') > # page.html.should match(/About/i) # no help > # page.should match(/About/i) # no help > end > end > > > Failures: > > 1) PagesController GET ''about'' should be successful > Failure/Error: visit pages_about_path > LoadError: > no such file to load -- action_controller/integration > # ./spec/controllers/pages_controller_spec.rb:10:in `block (3 > levels) in <top (required)>''Not sure about this part. Looks like it thinks its an integration test (which it is not - it''s an ActionController::TestCase). HTH, David