How do I tell cucumber where to search for files? - ruby-on-rails

In my cucumber step definitions, I want to use constants defined in "#{Rails.root}/config/initializers/constants.rb" and FactoryGirl factories defined in #{Rails.root}/spec/support/factories". How do I configure cucumber to look in those places?

Files in the initializer are loaded automatically when the Rails application starts. The same applies to factories, as long as the factory_girl_rails is added as dependency to the environment that is used by cucumber to run the scenarios (generally cucumber).

Related

How to tell RSpec to restart Rails before an example is run?

Is it possible to tell rspec to restart Rails before an example is run? I'm building an Engine that hooks into the Rails initialization process and the users can make some configuration changes, in an initializer, that impact how Rails and the Engine are configured. I want to be able to simulate those configuration changes, restart rails and test the result.
I haven't done this feat yet, but as best practice I think your engine tests should be part of the engine and should have minimal dependencies.
Some approaches I've seen and believe you should try and combine:
Mock a minimal parent rails app to test your engine.
Write multiple dummy apps to test with.
Instead of loading the entire rails application, you can split spec_helper and rails_helper in smaller parts, also gaining in setup time.
You can write custom rake tasks to switch environment before spawning a new test thread.
You can also overwrite at runtime the configuration values which reflect in your test (plus: use dependency injection!).
If your initializer is complex enough, you could extract it in a testable helper and wire it up in your test initializers.
Also, there seems to be a gem for that: Combustion.

How to generate cucumber-rails for a different folder

I'm trying to create a test folder for my rails project using cucumber-rails generator (the gem is here: https://github.com/cucumber/cucumber-rails ).
The problem is that when I run "rails generate cucumber:install" it creates a folder in my project named "features" with the cucumber files inside that folder, which is - in my perspective - not very organized.
I would like all the cucumber files to be inside a folder named "test". Is there a way that allows me to do that? I tried to run the generator inside the "test" folder but it didn't seem to work.
Anyone knows how to do this? Thanks a lot!
There are essentially 3 test libraries in Ruby and corresponding common assumptions:
test is expected to contain Test::Unit or MiniTest tests
spec is expected to contain RSpec tests
features is expected to contain cucumber tests
You should follow these assumptions or you may end up having several troubles in the long run.
In any case, to answer your question, what you want to do is not possible. The features folder is hard-code in the generator and in the cucumber task file. Therefore, unless you want to introduce several hacks and workaround, you'll have to follow the conventions.
I think there is a pretty straight forward solution for this:
In an issue it is pointed out that we can point to different paths for our features in the cucumber.yml
default: features
// becomes
default: test/features
But, then it can't find the step_defintions if you also add your custom path as a required flag then it can pick up the step_defintions as well.
default: features
// becomes
default: test/features -r test/features/step_definitions
if you don't want to use cucumber.yml you can use the cucumber cli to take care of it:
bundle exec cucumber test/features -r test/features/step_definitions
NOTE: I am using this in a docker environment and this works fine for both local and docker implementations.

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.

What's the difference b/w minitest-rails and minitest-spec-rails?

Looking into using Minitest for an existing Rails 3.2.
I'm trying to understand the difference between minitest-rails and minitest-spec-rails.
The main differences between minitest-spec-rails and minitest-rails is the scope of the problems they solve.
minitest-spec-rails
minitest-spec-rails overrides the test infrastructure that Rails provides by default and adds the ability to use the Minitest Spec DSL and matchers in your tests. This means that you don't have to make any changes to your existing tests to start using the Spec DSL. It also means that the Spec DSL is available in every test.
minitest-rails
minitest-rails adds the ability to use the Spec DSL, but also changes the approach to how tests are written. It provides new generators for your tests, and allows you to choose from the TDD-style assertions or the BDD-style expectations. It places test files in more sensible locations.
It also allows your existing tests to live side by side with your new Minitest tests. You have the option to override the default test infrastructure similar to minitest-spec-rails, but you also have the option to leave them untouched.
Disclosure: I am the author of minitest-rails
With minitest-spec-rails, we have a working solution by replacing MiniTest::Spec as the superclass for ActiveSupport::TestCase. This solution is simple and does not require you to recreate a new test case in your test_helper.rb or to use generators supplied by gems like minitest-rails.
Minitest changes for testing within Rails. Your test classes will inherit from MiniTest::Rails::ActiveSupport::TestCase a opposed to ActiveSupport::TestCase. You can use the MiniTest::Spec DSL. You can generate test files with the standard model, controller, resource, and other generators.
rails generate model User
or
And you can specify generating the tests using the MiniTest::Spec DSL on any of the generators by providing the --spec option
rails generate model User --spec

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.

Resources