0

我正在尝试使用 RSpec on Ruby on Rails 测试我的应用程序控制器。我没有使用 Capybara(因为很多人都使用它)。这是我的规格测试:

require 'spec_helper'

describe UserController do

it "create new user" do
    get :create, :user => { :email => 'foo@example.com', :name => 'userexample' }
    flash[:notice] = 'new user was successfully created.'
end
  describe "signup" do

  before { visit new_user_registration_path }

  let(:submit) { "Create my account" }

  describe "with invalid information" do
  it "should not create a user" do
    expect { click_button submit }.not_to change(User, :count)
  end
 end

describe "with valid information" do
  before do
    fill_in "Name",         :with=> "Example User"
    fill_in "Email",        :with=> "user@example.com"
    fill_in "Password",     :with=> "foobar"
    fill_in "Confirmation", :with=> "foobar"
  end

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

这是我的Usercontroller

class UserController < ApplicationController
def index

end

def new
     @user = User.new
end

def create
    @user = User.new(params[:user])
    if @user.save
        redirect_to user_session_path
    else
    redirect_to new_user_session_path
end

end

def show
    @user = User.find(params[:id])
    #redirect_to @user
end
end

当我测试它时,我得到了错误undefined method 'visit'::

Failure/Error: before { visit new_user_registration_path }
 NoMethodError:
   undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_2:0x132cefbc0>
 # ./spec/controllers/user_controller_spec.rb:11
4

1 回答 1

0

您必须使用 Capybara 来执行此功能。我认为您的测试不在正确的位置。你必须为此做请求规范。它不适用于控制器规格。请参阅文档:https ://github.com/rspec/rspec-rails/ 。

于 2012-07-23T19:09:29.393 回答