[rspec-users] [HELP] should receive :new but does not

Camilo Torres xtrmclmb at gmail.com
Tue Jul 22 21:36:12 EDT 2008


Hello, I am learning rspec/rspec-rails and ruby on rails.

Controller:
class IssuesController < ApplicationController
	def index
		
	end
	
	def new
		@issue = Issue.new
	end
	
	def create
		@issue = Issue.new(params[:issue])
		@issue.save
	end
end

Spec:
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper.rb'))

describe IssuesController do
	it 'should sucess on index' do
		get :index
		response.should be_success
	end
	
	describe 'create new issue' do
		it 'should success on new' do
			get :new
			response.should be_success
		end
	
		it 'should pass a new Issue to the view' do
			issue = mock_model(Issue)
			Issue.stub!(:new).and_return(issue)
			get :new
			assigns[:issue].should == issue
		end
	end
	
	describe 'saving with right data' do
		it 'should save the issue' do
			issue = mock_model(Issue)
			Issue.stub!(:new).and_return(issue)
			issue.stub!(:save).and_return(:true)
			post :create, { :issue => {:title => 'some title', :description =>
'some more data to describe'}}
			assigns[:issue].should == issue			
		end
	end
	
	describe 'when saving wrong data' do
#		before(:each) do
#			@issue = mock_model(Issue)
#			@issue.stub!(:save).and_return(:false)
#			Issue.stub!(:new).and_return(@issue)
#		end
		
#		def post_create
#			post :create, { :issue => {:title => 'some title', :description =>
'some more data to describe'}}
#		end
		
		it 'should not save the issue' do
			@issue = mock_model(Issue)
			@issue.stub!(:save).and_return(:false)
			Issue.stub!(:new).and_return(@issue)
			
			post :create, { :issue => {:title => 'some title', :description => ''}}
			
			@issue.should_receive(:save).and_return(:false)
		end
		
#		it 'should flash the failure to save' do
			
#		end
	end
end

Error:
F....

1)
Spec::Mocks::MockExpectationError in 'IssuesController when saving
wrong data should not save the issue'
Mock 'Issue_1001' expected :save with (any args) once, but received it 0 times
spec/controllers/issues_controler_spec.rb:51:
spec/controllers/issues_controler_spec.rb:3:

Finished in 0.282526 seconds

5 examples, 1 failure



I am running script/autospec.

¿What is wrong? I don't understand why this spec is not passing. I
have tried before(:each) and a helper method, but got the same.

Please, help.


More information about the rspec-users mailing list