Integration tests: 3 questions


#1

Hello,

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/#
    {foo.id}”}
    end

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

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

  1. 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
    end
    end
    check_news_links
    end

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| -%>

<%= month.strftime('%B %Y')%>

    <% for item in items %>
  • <%=link_to item.title, news_page_path(item) %>
  • <% 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.

  1. 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.

Thanks,
Elle


#2

I changed my search method to be apart of index:
def index
if params[:search]
@products = Search.products.with(params[:search])
else
@products = Product.find_valid
end
end

And changed my test accordingly and it passes.

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

Thanks,
Elle