# Rspec - has\_many-belongs\_to controller question

**URL:** <https://www.ruby-forum.com/t/rspec-has-many-belongs-to-controller-question/141036>\
**Category:** Rails\
**Created:** [June 21, 2008, 12:21am UTC](https://www.ruby-forum.com/t/rspec-has-many-belongs-to-controller-question/141036 "2008-06-21T00:21:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [June 21, 2008, 12:21am UTC](https://www.ruby-forum.com/t/rspec-has-many-belongs-to-controller-question/141036/1 "2008-06-21T00:21:16Z")

</div>

I’m using  
restful-authentication from

> **[GitHub - technoweenie/restful-authentication: inactive project](https://github.com/technoweenie/restful-authentication/tree/master)**
>
> master

RSPEC 1.1.4 from [Home · dchelimsky/rspec-rails Wiki · GitHub](http://github.com/dchelimsky/rspec-rails/wikis/home)  
(downloaded this week)

The spec for loging in work  
I even have it working checking for authentication on the teachers  
controller.  
What I want to do is find students that belong to a teacher  
So if I’m the teacher and I log in I want to see only my students.

* * *

Models

class Students \< ActiveRecord::Base  
belongs\_to :teacher  
end

class Teacher \< ActiveRecord::Base  
has\_many :students  
end

* * *

controller

class TeachersController \< ApplicationController

# GET /teachers

# GET /teachers.xml

def index  
@teachers = current\_user.projects

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @teachers }
end

```

end  
end

* * *

controler\_spec

require File.expand\_path(File.dirname( **FILE** ) + ‘/…/spec\_helper’)

describe StudentsController do  
describe “handling GET /students” do

```
before(:each) do
  @student = mock_model(Student)
  student.stub!(:find).and_return([@student])
  @User = user_login # logs user in
end

def do_get
  controller.logged_in?().should == true
  get :index
end

it "should be successful" do
  do_get
  response.should be_success
end

it "should render index template" do
  do_get
  response.should render_template('index')
end

it "should find all students" do
  Student.should_receive(:find).with(:all).and_return([@student])
  do_get
end

it "should assign the found projects for the view" do
  do_get
  assigns[:student].should == [@student]
end

```

end  
end

* * *

errors I get

Spec::Mocks::MockExpectationError in ‘StudentsController handling GET /  
students should assign the found students for the  
view’  
Mock ‘Person\_1155’ received unexpected message :students with (no  
args)  
C:/InstantRails-2.0-win/rails\_apps/ztemp/app/controllers/  
students\_controller.rb:5:in `index' ./spec/controllers/students_controller_spec.rb:14:in `do\_get’  
./spec/controllers/students\_controller\_spec.rb:35:

NoMethodError in ‘StudentsController handling GET /students should  
find all students’  
undefined method `authenticate_with_http_basic' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1::Subclass _1:0x43914b8> C:/InstantRails-2.0-win/rails_apps/ztemp/lib/authenticated_system.rb: 111:in `login\_from\_basic\_auth’  
C:/InstantRails-2.0-win/rails\_apps/ztemp/lib/authenticated\_system.rb:  
12:in `current\_person’  
./spec/controllers/students\_controller\_spec.rb:29:

Spec::Mocks::MockExpectationError in ‘StudentsController handling GET /  
students should render index template’  
Mock ‘Person\_1159’ received unexpected message :students with (no  
args)  
C:/InstantRails-2.0-win/rails\_apps/ztemp/app/controllers/  
students\_controller.rb:5:in `index' ./spec/controllers/students_controller_spec.rb:14:in `do\_get’  
./spec/controllers/students\_controller\_spec.rb:23:

Spec::Mocks::MockExpectationError in ‘StudentsController handling GET /  
students should be successful’  
Mock ‘Person\_1161’ received unexpected message :students with (no  
args)  
C:/InstantRails-2.0-win/rails\_apps/ztemp/app/controllers/  
students\_controller.rb:5:in `index' ./spec/controllers/students_controller_spec.rb:14:in `do\_get’  
./spec/controllers/students\_controller\_spec.rb:18:

* * *

I think this is what is giving me grief

Student.should\_receive(:find).with(:all).and\_return([@student])

but I don’t know how to get it to work

Any help will be appreciated

John I
