How does rspec work with Rails 3 for integration tests? - ruby-on-rails

What I'm trying to ahieve is to do integration tests with webrat in rails3 like Yehuda does with test-unit in http://pivotallabs.com/talks/76-extending-rails-3 minute 34.
an example:
describe SomeApp
it "should show the index page"
visit "/"
body.should =~ /hello world/
end
end
Does someone knows a way to do it?

Have you tried using rspec-rails: http://github.com/rspec/rspec-rails
That is the repository (install instructions in the Readme) for the new RSpec 2.0 which will work with Rails 3.

thoughtbot just published a blog post on this.
Stability can become an issue as web applications evolve and grow — integration tests provide a great way to perform end-to-end tests that validate the application is performing as expected.
— RSpec integration tests with Capybara for end-to-end testing

Support for Rails 3 is coming in rSpec 2, apparently.

ceck this link now it is very simple tu use rspec with rails3
http://github.com/rspec/rspec-rails and you don't need to run script/generate rspec_scaffold like in rails 2.x.x, now you just run rails g scaffold and it will generate the rspec files

Related

How to run an integration test in RoR with rspec and capybara

I have a good understanding of the differences between unit and intergration tests in RoR in theory. I'm using rspec and capybara to do a lot of testing on my site. What I don't understand is how do you run different tests? If I do
bundle exec rspec
it will run all of my specs in all of my spec folders (model, controller, views, integration, etc). If I want to run an integration test, is it as simple as ?
bundle exec rspec spec/integration
I know there are some differences between these types of test behind the scenes. Specifically this problem (which I also have) has me thinking about unit vs. integration: How to test for a redirect with Rspec and Capybara
What does Rails do differently when running integration tests? The solution posted in the above problem is
Checking for redirect is not supported in rspec-rails request specs,
but is supported in Rails integration tests.
So how do I make one of my tests an integration test? Is it just a matter of putting it in the right spec folder?
As I understand it rspec is designed specifically for unit testing purposes, thus you'll need to use something else for integration testing. The message seems it imply that Rails can do integration tests as well. I don't have any experience or knowledge of testing in rails alone, but I do know of the cucumber gem that is built very well for integration tests.
It has it's own domain specific language and other quirks you'll need to get used to but it should have the capability you're looking for.

Alternative to current Ruby on Rails testing methods?

I find that in order to thoroughly test a Rails application with Rspec I am required to write more test code than actual functional Ruby code. Call me crazy but this does not seems right. Is there a different/alternate approach (even one that is not as comprehensive as Rspec).
For unit testing I guess you won't find any replacement.
But for integration testing, you could create scenarios within your browser thanks to Selenium. See: http://seleniumhq.org/
There are many option available here are the few list
Rspec
Watir
Cucumber
factorygirl
capybara
and many more

How to add first Cucumber test to a Rails app

Confession: I have never written a single test for Rails.
I have installed the gems cucumber, rspec, capybara, factory girl. Running Rails 3.1.
I am not sure, um, where to create a new test file or what to name it.
Thanks for your patience.
Micheal Hartl has a good tutorial on Rails that is mostly test driven:
http://ruby.railstutorial.org/
You probably know most of this but it will point you in the right direction.
Here's a Rails Cast on Cucumber:
http://railscasts.com/episodes/155-beginning-with-cucumber
Here's an RSpec Rails Cast:
http://railscasts.com/episodes/71-testing-controllers-with-rspec
Here are a bunch of Cucumber examples:
https://github.com/cucumber/cucumber/tree/master/examples/i18n
Hope that helps!
after installing the rspec and cucumber
you must run following commands
rails generate rspec:install for rspec
first command
will configure rails generate command and it will create the spec directory which will contain tests for your models, controllers, views in respective directory you can write the
rspec test
eg. If you are having user model then specs for user will go in
spec/models/user_spec.rb
that's it
to run these tests use
rspec spec/models/user_spec.rb
which will output the whether the tests are passed or not
cucumber describes the behavior of application
and rspec describes behavior of object
rails generate cucumber:install for cucumber
which will create features directory in your application root
inside that you can write cucumber test with .feature extension
eg. If your application have feature like creating user, this feature will go in
features/creating_user.feature file
and the step definition for this feature will go in
features/step_definitions/create_user_steps.rb
well its just short guide line you can refer the following links
for cucumber
http://loudcoding.com/posts/quick-tutorial-starting-with-cucumber-and-capybara-bdd-on-rails-project/
Think what is the most common way for people to use your app. Write a test for the 'happy path', ignoring any edge cases.
Next, write tests for the parts most likely break.

Rails acceptance testing without using rspec but using capybara

I am looking for an example of acceptance testing in Rails without using rspec. My current client uses ruby 1.9.2 and all the tests are written using minitest and they are concerned about the slowness of rspec. Anyways that discussion is over.
I need to write a few functional tests and am looking for examples of how to do functional tests without using rspec. And I would like to use capybara.
I am using Rails 3.0.10 .
sounds like you want to be looking at Cucumber - http://cukes.info/ perhaps?

Rspec integration tests without cucumber?

Is there a way to do integration tests with Rspec without using Cucumber? I prefer using just plain old Webrat. Thanks.
The latest version of RSpec-Rails (1.2.7) now has integration support. Upgrade then start adding specs to spec/integration or use the 'integration_spec' generator. Configure Webrat in spec/spec_helper.rb and you're set!
We've recently started using RSpec with Capybara over Cucumber. Here is a "beginners" blog post I recently wrote on using RSpec integration tests without cucumber.
End-to-end testing with RSpec integration tests and Capybara
Let me know if you have any questions on getting your system set up.
As far as I know Rspec is perfectly capable of testing views and controllers as part of integration tests. A quick look around the internet shows this article at Robby on Rails on view testing and some of the Rdocs within RSpec might help.
Hope this points you in the right direction - I'm afraid I use cucumber myself.

Resources