Where/How to Include Helper Methods for Capybara Integration Tests

Where/how to include helper methods for capybara integration tests

Put your helper to the spec/support folder and do something like this:

spec/support/:

module YourHelper
def register_user(user)
visit home_page
fill_in 'user_name', :with => user.username
fill_in 'password', :with => user.password
click_button 'sign_up_button'
end
end

RSpec.configure do |config|
config.include YourHelper, :type => :request
end

Accessing view helper methods from Capybara rquest specs

You just need to include the relevant helper module in your describe block, and it will be available in all nested specs:

describe "something" do
include RequestSpecHelper
include ApplicationHelper

...
end

How to include/set visible helper methods for rspec?

If your generate_coordinate method is used by both your controller and helper, consider moving into your controller (as a private method) and adding this one-liner to allow views and helpers to access it:

# planets_controller.rb
helper_method :generate_coordinate

helper_method exposes controller methods to views and helpers within the scope of the controller (in this case, planets#index, planets#show, etc).

If you'd rather do it the other way round you have two options:

  • insert include PlanetsHelper at the top of the controller (under class PlanetsController)
  • when you want to call the helper method, call it like this: view_context.generate_coordinate(...)

Try them out and see which one suits your needs best.

Include helper module in Capybara specs

The helper methods can be included using RSpec.configure.

Assuming you want the helper methods available to all examples, add the following to your spec helper (or at least somewhere outside the example group):

RSpec.configure do |c|
c.include Features::SessionHelpers
end

For more examples, such as only adding the helper methods to specific examples, see the Relish's helper method page.

How to Include Rails views helper in Capybara steps

The way I include helper into my cucumber testing here:

  • You create a file world.rb (or any name you want) under support folder. In my world.rb I define like this:

    module Helper

    include ProjectHelper
    include ProductBacklogHelper
    include ApplicationHelper
    include Capybara::DSL
    include Prickle::Capybara

    end #end Module
    World(Helper)

You should include the helper file that has "number_to_currency" function in world.rb

I hope this will help you

Capybara custom Methods, where should these go?

When using RSpec your rails_helper.rb usually has a line

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

This loads up all the rb files in the spec/support directory when starting up. To add your methods you would add a new file, something like spec/support/capybara_react_helper.rb and in there put

module CapybaraReactHelepr
def a_helper_method(...)
...
end

def another_helper(...)
...
end
end

Then in your RSpec configuration (usually later in your rails_helper.rb) you can include those methods into feature tests

RSpec.configure do |config|
...
config.include CapybaraReactHelper, type: :feature
...
end

How to stub helper method in acceptance test with Capybara?

Are you able to stub the configuration instead? It's a very bad idea to stub your code in an acceptance test.

If you insist this is the best way, you can try:

ActionView::Base.any_instance.stub(:the_helper_method) { "other implementation" }

How to include/set visible helper methods for rspec?

If your generate_coordinate method is used by both your controller and helper, consider moving into your controller (as a private method) and adding this one-liner to allow views and helpers to access it:

# planets_controller.rb
helper_method :generate_coordinate

helper_method exposes controller methods to views and helpers within the scope of the controller (in this case, planets#index, planets#show, etc).

If you'd rather do it the other way round you have two options:

  • insert include PlanetsHelper at the top of the controller (under class PlanetsController)
  • when you want to call the helper method, call it like this: view_context.generate_coordinate(...)

Try them out and see which one suits your needs best.

Where to put integration tests?

spec/features is correct.

The generator is associated with the default Rails testing framework (which is not RSpec / Capybara).

Check https://www.relishapp.com/rspec/rspec-rails/docs/generators for Rspec generators.

Cheers!



Related Topics



Leave a reply



Submit