user2360003
user2360003

Reputation:

Undefined method "Get" and "Let" with Rspec 3 - rails 4

I've decided to start a new project using the new Rspec 3 (+ capybara/factory_girl) and am having trouble learning the new syntax. Right now I have

user_pages_spec.rb (Feature)

scenario "Signing Up" do

        let(:submit) { "Sign up" }

        scenario "With valid information" do
            background do
                fill_in "Username", with: "example"
                fill_in "Email", with: "[email protected]"
                fill_in "Password", with: "foobar123"
                fill_in "Password confirmation", with: "foobar123"
            end

            scenario "should create a user" do
                expect { click_button submit }.to change(User, :count).by(1)
            end
        end
    end

Fails with undefined method 'let'. And:

static_pages_spec.rb (controller)

describe StaticPagesController do

  describe "GET 'home'" do
    it "returns http success" do
      get :home
      expect(response).to be_success
    end
  end
end

with "undefined method 'get'. (This is just the default controller spec)

Upvotes: 0

Views: 633

Answers (2)

Myron Marston
Myron Marston

Reputation: 21800

You are getting undefined method let because capybara defines scenario an alias of it and feature as alias of describe. However, let is available in an example group context (a describe or context block) but not an individual example (and it block). So your example is equivalent to:

it "Signing Up" do
  let(:submit) { "Sign up" }

  it "With valid information" do
    background do
      fill_in "Username", with: "example"
      fill_in "Email", with: "[email protected]"
      fill_in "Password", with: "foobar123"
      fill_in "Password confirmation", with: "foobar123"
    end

    it "should create a user" do
      expect { click_button submit }.to change(User, :count).by(1)
    end
  end
end

...but should be:

feature "Signing Up" do
  let(:submit) { "Sign up" }

  context "With valid information" do
    background do
      fill_in "Username", with: "example"
      fill_in "Email", with: "[email protected]"
      fill_in "Password", with: "foobar123"
      fill_in "Password confirmation", with: "foobar123"
    end

    scenario "should create a user" do
      expect { click_button submit }.to change(User, :count).by(1)
    end
  end
end

Or, if you want to stick with pure RSpec constructs (rather than the capybara aliases):

describe "Signing Up" do
  let(:submit) { "Sign up" }

  context "With valid information" do
    before do
      fill_in "Username", with: "example"
      fill_in "Email", with: "[email protected]"
      fill_in "Password", with: "foobar123"
      fill_in "Password confirmation", with: "foobar123"
    end

    it "should create a user" do
      expect { click_button submit }.to change(User, :count).by(1)
    end
  end
end

Upvotes: 2

peter_v
peter_v

Reputation: 392

When upgrading existing project from RSpec 2.x to 3.0 had same problem.

It was fixed for me with an explicit setting of the type.

Could you try this:

describe StaticPagesController, type: :controller do

EDIT:

I found now that the more structural cause and solution is that in RSpec 3, I needed to add:

config.infer_spec_type_from_file_location!

in the config block in spec_helper.rb

Upvotes: 5

Related Questions