我試着去測試,其中在成功註冊成功的模板由以下控制器代碼Rspec的測試模板被渲染
def create
@user = User.new(params[:user])
if @user.save
render :template => "success"
else
flash[:notice] = "Oops Somethings not quite right! :("
render :action => "new"
end
end
我使用以下規範來測試這個代碼
before(:each) do
@user = User.new
@user.attributes = valid_attributes
@params = valid_attributes
@user.stub!(:save).and_return(true)
end
def do_post
post :create
end
it "should create new user " do
count = User.count
do_post
user = User.new(@params)
user.save.should eql(true)
User.count.should eql(count + 1)
end
it "should render the success page on successful signup" do
do_post
@user.save
response.should render_template("success") if @user.save
end
渲染的條件
但這個例子失敗「它應該在成功註冊成功渲染頁面」與此錯誤消息
1)
'UsersController handling POST /users should render the success page on successful signup' FAILED
expected "success", got "users/new.html.erb"
./spec/controllers/users_controller_spec.rb:67:
成功視圖是存儲在views/users /中的模板,無需執行任何操作。我猜我即將犯一個非常根本的錯誤,並希望得到一些幫助。
我會刪除user.save條件最後一個斷言。 – Rimian 2013-04-15 02:27:23