# Translating this test using mocha

**URL:** <https://www.ruby-forum.com/t/translating-this-test-using-mocha/127714>\
**Category:** Rails\
**Created:** [January 31, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/translating-this-test-using-mocha/127714 "2008-01-31T18:14:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [January 31, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/translating-this-test-using-mocha/127714/1 "2008-01-31T18:14:47Z")

</div>

Hello,  
I’m trying to learn Mocha and having a bit of problems how to  
translate some of my tests to the mocha world.

For example - I’m trying to convert this simple test:

[code]  
def test\_get\_feed\_details\_w\_bad\_url  
assert\_incomplete\_test  
get :feed\_widget\_details, :k =\>  
‘e6efb203a1cdebcf5a7406175924fe9991979f68’, :id =\> artists(:first).id  
assert\_response :success  
assert\_equal WEB\_SERVER + ‘/controller/rss/blog/artist\_1’,  
assigns(:blog\_url)  
assert\_include ‘No blog entries’,  
@response.body  
end

[code]

What would be the mocha form of this?

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [February 3, 2008, 11:04am UTC](https://www.ruby-forum.com/t/translating-this-test-using-mocha/127714/2 "2008-02-03T11:04:08Z")

</div>

Clem R. wrote:

> ‘e6efb203a1cdebcf5a7406175924fe9991979f68’, :id =\> artists(:first).id  
> assert\_response :success  
> assert\_equal WEB\_SERVER + ‘/controller/rss/blog/artist\_1’,  
> assigns(:blog\_url)  
> assert\_include ‘No blog entries’,  
> @response.body  
> end
> 
> [code]
> 
> What would be the mocha form of this?

Not quite sure of the underlying implementation, so not sure I’m going  
to be much help, but it feels like there’s way too much stuff being  
tested here (Jay Fields has been done some great posts on this recently  
on his blog: [http://blog.jayfields.com/](http://blog.jayfields.com/) and uses Mocha in his examples),  
and also, possibly, some of the things being tested should be in a model  
(though not necessarily an ActiveRecord one)

First of all I’d test all the fetching and parsing of the feed in a unit  
test. Something like:

def test\_should\_raise\_bad\_url\_error

Net::HTTP.any\_instance.expects(:get).returns(Net::HTTPBadRequest.new(“1.1”,“400”,“Bad  
Request”)) # assuming your using Net::HTTP to do the fetching  
assert\_raise(Feed::BadRequestError) { Feed.new(:url =\>  
“[http://some.nonexistant.url](http://some.nonexistant.url)”)  
end

Then, in the functional test, it’s a lot simpler, cleaner, less brittle,  
etc

def test\_should\_show\_message\_when\_bad\_feed\_url  
Feed.expects(:new).raises(Feed::BadRequestError)  
get :feed\_widget\_details, :k =\>  
‘e6efb203a1cdebcf5a7406175924fe9991979f68’, :id =\> artists(:first).id  
assert\_response :success  
assert\_select “title”, “No blog entries”  
end

I’ve not included the testing of the :blog\_url instance variable, as  
this is presumably set on all request, and should have its own test.

Hope this helps

Chris

## –

[http://pushrod.wordpress.com](http://pushrod.wordpress.com) – old dogs, new tricks  
[http://autopendium.com](http://autopendium.com) – a place your old car can call home
