Forum: Ruby on Rails Integration tests: 3 questions

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
elle (Guest)
on 2009-04-23 14:03
(Received via mailing list)

I'm trying to write a couple of tests but keep getting errors or
failures and I really think it is because I am making a mistake
writing the test, rather than the actual app (just cause it works when
I check it). Anyhow, here goes.

1. I am using EasySearch plugin. When I try to test that search works
fine, I did:
def searches_for_foo
      foo = Search.products.with("foo")
      get '/catalog/search'
      assert_response :success
      assert_template "search"
      assert_equal 1, Search.products.with("foo").size
      assert_tag :tag => "h1", :content => "Search Results for #{foo}"
      assert_tag :tag => "a", :attributes => { :href => "/catalog/#

And in the catalog controller:
def search
    @search_results = Search.products.with(params[:search_string])
    unless @search_results.size > 0[:notice] = "No products found matching your criteria"

Error says:
Expected response to be a <:success>, but was <500>
<"You have a nil object when you didn't expect it!\nThe error occurred
while evaluating nil.match">

I understand that it does not see the phrase I am searching for.
Because I get the same error is I go to /catalog/search manually. But
I don't know how to:
a) fix the test
b) fix the controller so I will not see this exception page

2. I have a model for pages with a column whether it is a news pages
or not (boolean)
My test is:
def views_news
      get "/news"
      assert_response :success
      assert_template "index"
     @news_months.each do |month, items|
        assert_tag :tag => "h3", :content => month
        for item in items
          assert_tag :tag => "li", :content => item.title

The controller has the following:
@news_months = @news.group_by { |t| t.created_at.beginning_of_month }

And the viewer has:
<% @news_months.each do |month, items| -%>
  <h3><%= month.strftime('%B %Y')%></h3>
  <% for item in items %>
    <li><%=link_to item.title, news_page_path(item) %></li>
  <% end -%>
<% end -%>

Error says:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Now, once again, I understand the error. What I don't know is how to
change the test so it can see each news item.

3. This is more a theoretical question but I wanted your advice on how
much should I test for? how small a detail should I test for?

I know the above are a lot of questions. Really appreciate your help.

elle (Guest)
on 2009-04-24 04:28
(Received via mailing list)
I changed my search method to be apart of index:
def index
    if params[:search]
      @products = Search.products.with(params[:search])
      @products = Product.find_valid

And changed my test accordingly and it passes.

Can anyone please have a look at my views_news test??

This topic is locked and can not be replied to.