M
M
Max2016-02-28 19:23:14
Ruby on Rails
Max, 2016-02-28 19:23:14

How to understand controller testing in Rails?

Hello, I will be grateful if you explain what this controller test does.
I do the lesson. Before that, everything is clear and understandable, but in testing I just started to disassemble it and I don’t understand a little what is happening here.

test "Invalid category submission results in failture" do
    get new_category_path
    assert_template 'categories/new'
    assert_difference 'Category.count', 1 do
      post_via_redirect categories_path, category: {name: " "}
    end
    assert_template 'categories/new'
    assert_select "h2.panel-title"
    assert_select "div.panel-body"
  end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Beloshitsky, 2016-02-28
@maxprof

Everything happens like this:
Go to the page at the address new_category_path
Make sure that this page was rendered using the template at the addressapp/categories/new.html.erb

assert_difference 'Category.count', 1 do
  post_via_redirect categories_path, category: {name: " "}
end

We make sure that when sending a POST request to the address categories_path, a new category is actually created.
See above.
assert_select "h2.panel-title"
assert_select "div.panel-body"

We make sure that there are elements h2.panel-titleand in the DOM of the page div.panel-body(here, most likely, a check that the form was made using one well-known css framework).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question