0

我决定使用新的 Rspec 3 (+ capybara/factory_girl) 开始一个新项目,但在学习新语法时遇到了麻烦。现在我有

user_pages_spec.rb(功能)

scenario "Signing Up" do

        let(:submit) { "Sign up" }

        scenario "With valid information" do
            background do
                fill_in "Username", with: "example"
                fill_in "Email", with: "example@example.com"
                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

由于未定义的方法“让”而失败。和:

static_pages_spec.rb(控制器)

describe StaticPagesController do

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

带有“未定义的方法'get'。(这只是默认的控制器规范)

4

2 回答 2

5

将现有项目从 RSpec 2.x 升级到 3.0 时遇到了同样的问题。

它通过明确的类型设置为我修复。

你能试试这个:

描述 StaticPagesController,类型: :controller do

编辑:

我现在发现更多的结构性原因和解决方案是在 RSpec 3 中,我需要添加:

config.infer_spec_type_from_file_location!

在 spec_helper.rb 的配置块中

于 2014-06-09T14:36:40.330 回答
2

你得到undefined method let是因为 capybara 定义scenario了一个别名itfeature作为describe. 但是,let在示例组上下文(adescribecontext块)中可用,但在单个示例(和it块)中不可用。所以你的例子相当于:

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

  it "With valid information" do
    background do
      fill_in "Username", with: "example"
      fill_in "Email", with: "example@example.com"
      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

...但应该是:

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

  context "With valid information" do
    background do
      fill_in "Username", with: "example"
      fill_in "Email", with: "example@example.com"
      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

或者,如果您想坚持使用纯 RSpec 构造(而不是 capybara 别名):

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

  context "With valid information" do
    before do
      fill_in "Username", with: "example"
      fill_in "Email", with: "example@example.com"
      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
于 2014-06-13T23:01:21.933 回答