How to Run Cucumber and RSPEC? - ruby-on-rails

right now I use rspec for my tests. to run rspec all I have to do is:
rake spec
I now want to use Cucumber to write tests for making sure the paths for sign in and sign up work correctly with Devise & Omniauth.
To use Cucumber I need to write tests in /features right?
So does that mean to test I need to run
rake spec
And then run another command
cucumber
So now I have to run two testing frameworks?
Thanks

Adapted from http://blog.andywaite.com/2013/02/28/rakefile-cucumber-rspec-jasmine/
Rakefile:
require 'rspec/core/rake_task'
require 'cucumber/rake/task'
RSpec::Core::RakeTask.new
Cucumber::Rake::Task.new
task :default => [:spec, :cucumber]
But I prefer guard to automatically run tests.

Yes. One is a unit testing framework, the other is for high level acceptance tests.
Also, if you are looking for integration tests rather then acceptance tests (which is what it sounds like), plain capybara with rspec will probably be a better fit (https://github.com/jnicklas/capybara, scroll down to capybara + rspec)

Yes that's correct. Generally speaking I use Rspec for unit testing, and cucumber for integration. If you find RSpec is covering integration tests for you, and you dont need to write cucumber features to spec out the app with a client or systems analyst, then perhaps cucumber is not for you. Personally, i love how cucumber works for integration testing. It feels very reusable, and it's easy to understand what's going on in a test.

To address your specificity about running 2 commands, running autotest to automatically run your tests whenever you make a change to the spec code or program code removes the need to use the rake commands.
https://github.com/dchelimsky/rspec/wiki/Autotest-Integration
If you use cucumber the running AUTOFEATURE=true autotest will run rspec tests and cucumber specs in sequence
https://github.com/cucumber/cucumber/wiki/Autotest-Integration
Autotest has a great function in that it runs the whole spec suite but, if any tests fail it will rerun only that test until it passes, and then reruns the entire test again. It also has the added value of randomizing the order of tests so that it ensures the tests are valid in isolation

Related

Rspec & Capybara test pass isolated, fail running together

When I run the JS feature tests in isolation they work fine.
When I run the test suite, then they fail.
I execute '--bisect' with 'rspec spec' and return 1 test that produce the fail.
spec:
https://gist.github.com/lfgdz/c56481ce88cdb80750fa6d98f0ec1a4d
spec_helper:
https://gist.github.com/lfgdz/08b1b82d2a36aaf7929af79b04c652c1
When specs pass in isolation but fail when run together, there must be some interdependency or information that's leaking between them.
As #slowjack2k said, your spec_helper.rb specifies:
config.use_transactional_fixtures = false
Per the RSpec Transactions doc, that means you've asked RSpec not to wrap each spec in a transaction. This means changes to the database made during one spec can impact others, including between standard unit tests and Capybara feature tests.
If you really need transaction fixtures turned off, you'll need to use database_cleaner or some other mechanism to ensure that changes made in one spec don't interfere with other specs.

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.

what are the required Gem for Automated Acceptance testing

I want to write acceptance testing through cucumber and capybara. i have experience with rspec. I want to write integration/features test cases . I want outcome to be seen on a web browser which shows the test case running. what are the gems required for writing test cases .
If you are already familiar with RSpec, I recommend you to use RSpec with Capybara for acceptance testing. RSpec is testing framework, and Capybara is a library that helps you to test web applications by simulating how a real user would interact with your app.
Acceptance tests in RSpec with Capybara are called "feature specs" and live in /spec/features directory in Rails. Capybara comes with a built in DSL for writing descriptive acceptance tests with RSpec (feature/scenario), but you may also use traditional RSpec DSL (describe/it).
Capybara supports several drivers, and its default driver RackTest doesn't support JavaScript. So, probably, you'll want to use one of the alternative drivers (I prefer Poltergeist, but it is headless, so if you want to see result in the browser, you may use Selenium driver). In this case, you'll need to set up database_cleaner to clear your database between tests. Capybara's README contains a lot of information about its configuration and usage with RSpec, configuring driver and using database_cleaner.
You can start with this and this screencasts. But remember, that they are a little bit outdated and use traditional RSpec DSL (instead of new Capybara DSL), and use old convention, when "feature" specs were called "request" specs. Currently, by convention, "request" specs, are integration tests without capybara. So you'll need to create your capybara specs in spec/features directory, not spec/requests. And if you want to use Capybara DSL, this is easy to fix too. Just replace describe with feature, it with scenario, let with given etc. It is well documented in Capybara's README.
Hope this helps.

(Rails) PaperTrail and RSpec

I'm having trouble with PaperTrail (auto-versioning of objects for Rails) being used with RSpec tests. Normally I want my tests to run without PaperTrail versioning, but there are a handful of tests for which I want PaperTrail turned on. I typically run my tests with Guard and Spork, and I can use things like PaperTrail.enabled = true and PaperTrail.enabled = false around a given test and everything works fine.
However, when I run the tests with RSpec, the tests requiring PaperTrail fail. To be more specific, it appears that while code in before filters can produce versions objects, code in the tests cannot. After a considerable amount of digging and tinkering and trying code snippets (I've tried this and this), it looks like the best solution is to use the require "paper_trail/frameworks/rspec" line mentioned in the PaperTrail README.
Unfortunately, each of these keeps me right where I started—tests pass with Guard/Spork but not vanilla RSpec. This is in particular an issue because while I use Spork locally, our continous integration server runs RSpec directly.
Does anyone have any insight?
PaperTrail now has documentation on tests with vanilla rspec
https://github.com/paper-trail-gem/paper_trail#7b-rspec
After including require 'paper_trail/frameworks/rspec' in your spec/rails_helpers.rb
... PaperTrail will be turned off for all tests by default. To enable PaperTrail for a test you can either wrap the test in a with_versioning block, or pass in versioning: true option to a spec block.
Somehow my issue was fixed by changing before(:all) and after(:all) behavior to before(:each) and after(:each).

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.

Resources