# RSPEC: Looking for some clarification

**URL:** <https://www.ruby-forum.com/t/rspec-looking-for-some-clarification/123068>\
**Category:** Rails\
**Created:** [December 13, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/rspec-looking-for-some-clarification/123068 "2007-12-13T20:20:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![akv292](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@akv292](https://www.ruby-forum.com/u/akv292)\
**Post date:** [December 13, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/rspec-looking-for-some-clarification/123068/1 "2007-12-13T20:20:01Z")

</div>

So, I am new to testing and am a fan of the RSPEC framework, so far.  
There are still a few things about mocking/stubbing that I am unclear  
on.

I am using the line…

Project.should\_receive(:find).with(@params[:id]).and\_return(@project)

…to test…

Project.find(params[:id])

… I am not sure what @params[:id] is referring to in my test. When  
I pass it in with(anything()) the test passes but with(@params[:id])  
it fails. I have created @params = {:id =\> “1”} and thought that  
would be enough, but it is not. If anyone can shed some light as to  
what exactly is going on here or at least point me in the right  
direction, I would very much appreciate it.

I will post my test in it’s entirety just in case it is a mistake  
somewhere else.  
I’m currently trying to test:

def front  
@project = Project.find(params[:id])  
end

The following code is my test:

describe Clients::ProjectsController, “testing the ‘front’ action” do

```
before(:each) do
  @staff = mock_model(User)
  Project.stub!(:staff).and_return(@staff)

  @project = mock_model(Project, :id => "1")
  Project.stub!(:find).and_return(@project)
  @params = { :id => "1" }
end

def do_post
  post :front, :project => @params
end

it "should find all of the projects with the params" do

```

Project.should\_receive(:find).with(@params[:id]).and\_return(@project)

# \<- this is my problem line

```
  do_post
end

it "should find all of the staff through the staff method" do
  Project.should_receive(:staff).with().and_return(@staff)
  do_post
end

it "should find the clients through the clients method"

```

end

---

<div class="post-metadata">

**Author:** ![akv292](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@akv292](https://www.ruby-forum.com/u/akv292)\
**Post date:** [December 14, 2007, 1:54am UTC](https://www.ruby-forum.com/t/rspec-looking-for-some-clarification/123068/2 "2007-12-14T01:54:25Z")

</div>

It looks like you are trying to test your model’s functionality by  
exercising the controller. If that’s not the case, then you need to  
tell your mock exactly what argument to expect. For example:

Project.should\_receive(:find).with(:all).and\_return([@project,  
@project])

or for a single id,

Project.should\_receive(:find).with(“1”).and\_return([@project, @project])

If this kind of testing can be pushed into model testing, that’s best.

Check out the rspec list:

[http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

HTH
