5

I have a simple test feature called test_feature_spec.rb:

(this is just for getting to the point of this SO question... the actual specs are much longer, actual feature specs)

require "features_helper"

RSpec.feature "Simple Feature", type: :feature do
  scenario "a test scenario" do
    get "/"
  end
end

I can do various Rails-y things, including capybara-driven things (like visit such_n_such_path). However the above crashes:

Failures:

  1) Simple Feature a test scenario
     Failure/Error: get "/"

     NoMethodError:
       undefined method `get' for #<RSpec::ExampleGroups::SimpleFeature:0x000000011a799788>
       Did you mean?  gets
                      gem

If I simply change it from type: :feature to type: :request it works just fine:

> rspec spec/features/test_feature_spec.rb
.

Finished in 0.64913 seconds (files took 5.82 seconds to load)
1 example, 0 failures

It seems that with type => request Rails or Rspec loads some stuff to allow calling controller routes, but it doesn't with type => feature.

Is there a way to load those helper methods (like get and post and such) but still leave the feature as type: :feature?

I know I could just change it to type: :request but technically these are feature specs, even though for some of them, to set up some state or do whatever, they need to call certain URLs.

Dan Sharp
  • 1,209
  • 2
  • 12
  • 31
  • It has been a while since I used Capybara, but I believe that you should use `visit` instead of `get` when using paths - can you try that? `get` is usually used with `:new` etc – kuwantum May 07 '18 at 23:08
  • `visit` worked, but my problem is that I want to be able to do direct HTTP calls of GET or POST (or whatever) type. `visit` is for driving an end-user browser like interaction. Anyway, I figured out an easy fix (see my answer below) – Dan Sharp May 08 '18 at 16:59

2 Answers2

1

Looks like there is a solution for this that works:

https://stackoverflow.com/a/24353475/1218280

The reason I asked this is a bit odd. I'm currently refactoring some Cucumber tests to be Rspec feature specs. Some of them were making get or post commands to endpoints in our app as "setup" steps. For example, there is an endpoint for enabling a certain flag in a DB record. I could just set up the DB object with the flag set, but I wanted to stay true to the original Cucumber code as much as possible (at least at first).

So now I can do this (and it works):

require "features_helper"

RSpec.feature "Simple Feature", type: :feature do
  scenario "a test scenario" do
    session = ActionDispatch::Integration::Session.new(Rails.application)
    response = session.get("/")
    # or I could do: session.post("/api/xyz", params: { one: "one", two: "two" })
  end
end
Dan Sharp
  • 1,209
  • 2
  • 12
  • 31
0

In your spec_helper you can try with:

RSpec.configure do |config|
  config.include FeaturesHelper, :type => :request
end
Sovalina
  • 5,410
  • 4
  • 22
  • 39