2012-07-23 47 views
0

我想用RSpec在Ruby on Rails上測試我的應用程序控制器。我沒有使用水豚(因爲許多人使用它)。這是我的天賦測試:爲什麼我會得到「未定義的方法`訪問」「?

require 'spec_helper' 

describe UserController do 

it "create new user" do 
    get :create, :user => { :email => '[email protected]', :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=> "[email protected]" 
    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 

回答

0

您必須使用水豚的這個功能。我認爲你的測試不在正確的地方。你必須爲此做請求規範。這不適用於控制器規格。請參閱文檔:https://github.com/rspec/rspec-rails/

+0

以及我必須測試控制器,什麼是請求規格? – Asantoya17 2012-07-23 19:15:17

+0

您是否在文檔中搜索過它?它與集成測試基本相同:http://guides.rubyonrails.org/testing.html#integration-testing。它用於測試視圖,控制器和模型之間的集成。 – Dougui 2012-07-23 19:21:05

+0

你是否知道我可以學習使用rspec測試的任何頁面,因爲我不知道這麼多。 – Asantoya17 2012-07-23 19:28:20

相關問題