Spec::Mocks::MockExpectationError

hii

I am initial level of rspec.I am getting a error nil class.

My Controller code is as follows

class UsersController < ApplicationController
before_filter :have_hard_drive?

# filter check for authorized user to access current server

before_filter :authorized_user_to_server?

# Will check that the cpanel id is belongs to current server or not

before_filter :is_cpanel_belongs_to_current_server?
def automatic_partitions
@prev_values = Cpanel.find(:all,:conditions => [“harddrive_id = ?
and server_id
=?”,HARDDRIVE_SLOTS[params[:harddrive_id].to_i],params[:server_id]])
@prev_values.each do |value|
value.destroy
end
@ignored_harddisk = IgnoredHarddrive.find(:first,:conditions =>
[“server_id = ? and harddrive_id =
?”,params[:server_id],HARDDRIVE_SLOTS[params[:harddrive_id].to_i]])
if @ignored_harddisk
@ignored_harddisk.destroy
end
end
end

and My Spec code is

require ‘spec_helper’
describe CpanelsController do

describe “role_as_superadmin” do
before(:each) do
user=User.authenticate(‘superadmin’,‘carmatec’)
user.is_superadmin
session[:user_id]=user.id
controller.stub(:have_hard_drive?)
controller.stub(:is_cpanel_belongs_to_current_server?)
controller.stub(:authorized_user_to_server?)

end
    it "should_test_automatic_partions" do

@prev_values=mock_model(Cpanel)
Cpanel.should_receive(:find).and_return(@prev_values)
 @ignored_harddisk=mock_model(IgnoredHarddrive)
 IgnoredHarddrive.should_receive(:find).and_return(@ignored_harddisk)

get :automatic_partitions,{:harddrive_id=>'sda',:server_id=>1938}
     assigns[:prev_values].should==''

end

end

and I am getting an error

Spec::Mocks::MockExpectationError in ‘CpanelsController
should_test_automatic_partions’
<Cpanel(id: integer, server_id: integer, mount_point: string,
file_system: string, size: string, grow: string, bad_sector: string,
raid_level: string, raid_device: string, harddrive_id: string) (class)>
expected :find with (any args) once, but received it 0 times

please anyone post the solution

On Nov 24, 2010, at 1:07 AM, Arun S. wrote:

# Will check that the cpanel id is belongs to current server or not

?",params[:server_id],HARDDRIVE_SLOTS[params[:harddrive_id].to_i]])

   it "should_test_automatic_partions" do

end
please anyone post the solution
My guess is one of the before filters is preventing the
automatic_partitions action from being invoked.

Cheers,
David

thanks for reply

my other methods in spec file it is working
properly(index,edit,update…)

but here it creates a probelm.bascially except CRUD opertion it is not
working.

get :automatic_partitions,{:harddrive_id=>‘sda’,:server_id=>1938}
assigns[:prev_values].should==’’

i simply want to ask that is this right way to call a action and pass
the value??

please post the reply as soon as.I am waiting for reply

On Thu, Nov 25, 2010 at 12:10 AM, Arun S. [email protected]
wrote:

i simply want to ask that is this right way to call a action and pass
the value??

please post the reply as soon as.I am waiting for reply

That seems like it should work, and the very first statement of the
controller action meets the expectation that is failing:

spec

Cpanel.should_receive(:find).and_return(@prev_values)

controller

@prev_values = Cpanel.find(:all, :conditions => [
“harddrive_id = ? and server_id = ?”,
HARDDRIVE_SLOTS[params[:harddrive_id].to_i],
params[:server_id]
])

I see the filters are stubbed in the before block. Try changing those
stubs to return true:

 controller.stub(:have_hard_drive?) { true }
 controller.stub(:is_cpanel_belongs_to_current_server?) { true }
 controller.stub(:authorized_user_to_server?) { true }

HTH,
David