[HELP] should receive :new but does not

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…

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.

The “should_receive” should appear before the “post :create” (look at
this for a better explanation ->
http://rubyforge.org/pipermail/rspec-users/2008-June/007342.html )

           it 'should not save the issue' do
                   @issue = mock_model(Issue)
                   @issue.stub!(:save).and_return(:false)

                   Issue.stub!(:new).and_return(@issue)
                   @issue.should_receive(:save).and_return(:false)

you should define the “should receive” before calling “post :create”

                   post :create, { :issue => {:title => 'some

title’, :description => ‘’}}
end

On Tue, Jul 22, 2008 at 10:36 PM, Camilo T. [email protected]
wrote:

           @issue = Issue.new

           end
           it 'should save the issue' do

before(:each) do

           it 'should not save the issue' do

Mock ‘Issue_1001’ expected :save with (any args) once, but received it 0 times

¿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.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208

thanks Maurício, I read your link. It worked.

2008/7/23 Maurício Linhares [email protected]:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs