# Method seems to work ok but the test fails - why?

**URL:** <https://www.ruby-forum.com/t/method-seems-to-work-ok-but-the-test-fails-why/115254>\
**Category:** Rails\
**Created:** [September 22, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/method-seems-to-work-ok-but-the-test-fails-why/115254 "2007-09-22T15:10:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 22, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/method-seems-to-work-ok-but-the-test-fails-why/115254/1 "2007-09-22T15:10:35Z")

</div>

Can anyone help me by explaining why this test fails?

An article has a title and a url (among other things). If we don’t get  
a title from the create article form, then i use hpricot and open-uri to  
look on the net and generate the title from the web page’s title. In  
the context of my app, it works fine (although the code is quite clumsy,  
i’m still a noob coder). However, a simple test that the article has a  
title is failing - in the tests, the constructor doesn’t seem to be  
doing its job properly.

here’s the test:

#article should generate a title from a url if not given a title  
def test\_makes\_a\_title\_from\_url  
article\_hash = {:url =\> “[www.google.co.uk](http://www.google.co.uk)”}  
article = Article.new(:article =\> article\_hash, :user =\> 1)  
assert\_equal(“iGoogle”, article.title)  
end

The test is failing because title is equal to nil. (normally i would  
just assert\_not\_nil, but i used assert\_equals here because i wanted the  
test to output what title _was_ equal to, in case it was some broken  
string or something)

Here’s the constructor:

##################################################################  
#constructor - takes the data from the article form (the  
#article/new view page), as a hash called :article, and the  
#current user, as :user  
##################################################################  
#params:

# - :article - a hash containing :url, :title, :tags

# - :user - a user, usually the current user (the person creating

#the article)  
##################################################################  
def initialize(params = {})  
super(params[:article])  
#first, check that the url is a valid web page  
begin  
web\_page = open(self.url)  
rescue #look for any exceptions  
#if it failed to validate the url, delete the object and return  
self.destroy  
return  
end  
#autogenerate the title from the web page’s title element if a  
#title is not given  
if self.title == “”  
self.title = ((Hpricot(web\_page))/“title”).first.inner\_html  
end  
#set some other values  
self.added\_at = DateTime.now.to\_s  
self.points = 0  
self.user\_id = params[:user]  
self.save  
#mark as scored up by the user who added it  
self.change\_score :user\_id =\> self.user\_id, :points =\> 1  
return true  
end

I thought it might be to do with hpricot/open-uri so i required them at  
the top of the test. But that hasn’t helped - i don’t even know if  
that’s the proper way to handle requires in testing. (Will the tests  
automatically use the requires and includes in my environment.rb? I  
don’t have anything in environment/test.rb besides the default stuff  
rails puts in there.)

This is probably something dumb, and obvious to most - can anyone see  
what i’m doing wrong?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 22, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/method-seems-to-work-ok-but-the-test-fails-why/115254/2 "2007-09-22T20:50:58Z")

</div>

Hi,

I’ve never used hpricot, so I thought I’d give this a try in irb. If I  
do

doc = Hpricot(open(“[www.google.co.uk](http://www.google.co.uk)”))

I get an error: “No such file or directory - [www.google.co.uk](http://www.google.co.uk)”

However, if I prepend “http://” to the url

doc = Hpricot(open(“[http://www.google.co.uk](http://www.google.co.uk)”))

it works. So, give that a try and let everyone know if that works.

I’d also like to offer this idea re: your Article constructor: it does  
too  
much. Fetching the title of a web page has nothing to do with an  
Article.  
Rather, an Article constructor should simply ensure that it is given a  
non-nil title, i.e., Article validates the presence of its title.  
Therefore,  
consider extracting a class whose purpose it is to build Articles, and  
give  
it the responsibility of using a given title or fetching one from the  
url to  
build an Article. Doing so will allow you to test Article in isolation  
from  
fetching a title from a url, resulting in shorter, more focused tests.  
Furthermore, separating these concerns will move your classes closer to  
having a single  
responsibility[http://en.wikipedia.org/wiki/Single\_responsibility\_principle](http://en.wikipedia.org/wiki/Single_responsibility_principle)[  
[Single-responsibility principle - Wikipedia](http://en.wikipedia.org/wiki/Single_responsibility_principle)], a design  
for  
which to strive.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 22, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/method-seems-to-work-ok-but-the-test-fails-why/115254/3 "2007-09-22T21:28:33Z")

</div>

Craig D. wrote:

> Hi,

> doc = Hpricot(open(“[www.google.co.uk](http://www.google.co.uk)”))
> 
> I get an error: “No such file or directory - [www.google.co.uk](http://www.google.co.uk)”
> 
> However, if I prepend “http://” to the url
> 
> doc = Hpricot(open(“[http://www.google.co.uk](http://www.google.co.uk)”))
> 
> it works. So, give that a try and let everyone know if that works.

Thanks craig - i spotted that as a potential problem and fixed it but  
the test still fails in the same way.

> Rather, an Article constructor should simply ensure that it is given a  
> non-nil title, i.e., Article validates the presence of its title.

I’m not very happy with my constructors generally. Ideally i’d like to  
do more research and then rewrite my app according to cleaner  
principles. I just dove in and started doing stuff, learning as i went  
along. I have learnt a lot, but i know now that most of my code is  
pretty clunky. (i’m a newcomer to programming and rails in particular).

Thanks for the advice 🙂

max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 22, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/method-seems-to-work-ok-but-the-test-fails-why/115254/4 "2007-09-22T21:58:44Z")

</div>

Craig D. wrote:

> If you can make your project available via anonymous checkout or some  
> kind  
> of download, I and/or someone else could try it and maybe offer a  
> solution.  
> Barring that, post as much code as you can, say your whole test and your  
> whole Article class.

> Doing is one of the best ways to learn, and you have a great community  
> willing to help when you get stuck. 🙂
> 
> max

Appreciate that Craig 🙂

I’m going to have a rethink about a few things before posting my class  
up, so i’ll leave it for now. Thanks for all your help though.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 22, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/method-seems-to-work-ok-but-the-test-fails-why/115254/5 "2007-09-22T21:48:06Z")

</div>

On 9/22/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > However, if I prepend “http://” to the url
> > 
> > doc = Hpricot(open(“[http://www.google.co.uk](http://www.google.co.uk)”))
> > 
> > it works. So, give that a try and let everyone know if that works.
> 
> Thanks craig - i spotted that as a potential problem and fixed it but  
> the test still fails in the same way.

If you can make your project available via anonymous checkout or some  
kind  
of download, I and/or someone else could try it and maybe offer a  
solution.  
Barring that, post as much code as you can, say your whole test and your  
whole Article class.

> Rather, an Article constructor should simply ensure that it is given a
> 
> > non-nil title, i.e., Article validates the presence of its title.
> 
> I’m not very happy with my constructors generally. Ideally i’d like to  
> do more research and then rewrite my app according to cleaner  
> principles. I just dove in and started doing stuff, learning as i went  
> along. I have learnt a lot, but i know now that most of my code is  
> pretty clunky. (i’m a newcomer to programming and rails in particular).
> 
> Thanks for the advice 🙂

Doing is one of the best ways to learn, and you have a great community  
willing to help when you get stuck. 🙂

max
