How to add first Cucumber test to a Rails app - ruby-on-rails

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.

Related

Rails Tutorial (M. Hartl) Chapter 3, Missing `static_pages_controller_test.rb` folder after `rails generate`?

I'm working on Michael Hartl's RoR Tutorial Chapter 3.2.1 and I'm generating a Static Pages controller using the command:
$ rails generate controller StaticPages home help
But after doing this I cannot see the see the test/controllers/static_pages_controller_test.rb file being generated like in the book (see tenth line):
$ rails generate controller StaticPages home help
create app/controllers/static_pages_controller.rb
route get 'static_pages/help'
route get 'static_pages/home'
invoke erb
create app/views/static_pages
create app/views/static_pages/home.html.erb
create app/views/static_pages/help.html.erb
invoke test_unit
create test/controllers/static_pages_controller_test.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke test_unit
create test/helpers/static_pages_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
Here's what it looks like when I run it:
My folder is also completely empty and cannot run any tests.
I think you may have mixed up editions of Michael Hartl's Ruby On Rails tutorial.
The 3rd edition uses Minitest instead of RSpec. However you app seems to be setup to use RSpec which is a different test framework which was used in the 2nd edition.
If you want to know why its generating RSpec specs instead of tests then its most likely because you ran:
rails generate rspec:install
Which changes the generators to generate specs instead.
The simplest way to remedy this is to delete your current sample_app directory and start over from the beginning of chapter 3 and make sure you only use the current edition:
https://www.railstutorial.org/book/static_pages
There are other ways but it might be easier to avoid other mishaps down the line if you get a fresh start.
Because you use rspec for testing and the generator command creates files and folders related to rspec suite. Instead of creating:
test/controllers/static_pages_controller_test.rb
it creates:
spec/controllers/static_pages_controller_spec.rb
so if you want to follow Michael Hartl's RoR Tutorial you must change your testing method to minitest.

How do I do fast unit testing in a Rails app without loading the Rails environment?

In a Rails app, I would like to have a setup that allows me to run fast unit tests of Rails independent application logic.
Have an alternative minimalistic test_helper.rb file where you just load minitest and make sure to require the code under test in your testcase file. That way you can run the test in isolation quickly and you can also run the test via Rake with the Rails environment loaded. See this gist for a code example.
There is explanation article about spec_helper.rb for rspec. You can use it analog for unit tests, of course.

Why won't my features specs run in RSpec?

In my Rails 4 app, I'm using Rspec for testing. My directory structure is
spec
-- controllers
-- factories
-- features
-- spec_helper.rb
-- support
When I run rspec spec, it runs my tests in controllers, but not in features. I can specify rspec spec/features and they'll run, but I want to be able to run all tests under spec at once. My guess is it's not looking in features because of a configuration setting, but I don't know where this is.
I've tried different incantations of starting rspec but haven't had any luck.
Based on your feedback to the comments above, the issue is one of file naming. I've definitely been burned by that before too. By default Rspec will go through the files looking for ones ending with _spec.rb, this default behaviour is overridden if you specify the folder manually.

List of RSpec's generators

I'm looking for a comprehensive list of RSpec's generators to easily generate specs for controllers, models, helpers, and so on. The only one I've found is:
rails g integration_test name
that saves a spec inside the spec/requests folder.
controller
helper
install
integration
mailer
model
observer
scaffold
view
example usage:
rails g rspec:integration events
--> create spec/requests/events_spec.rb
All the rspec-rails generators can be found at https://github.com/rspec/rspec-rails/tree/master/lib/generators/rspec You'll have to dig around in the code a little to see what they do, but they are well organized so it shouldn't be too much of a pain.
There's also a short readme on the generators which basically says that they are run automatically when you run one of the standard Rails generators (rails g model User):
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.

How does rspec work with Rails 3 for integration tests?

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

Resources