Used in automatically generated tests:
test "should create item" do login_user assert_difference('Item.count') do post :create, item: { creator: @item.creator, title: @item.title, user_id: @item.user_id, text: 'Hello, world!' } end assert_redirected_to(assigns(:item)) end
Rails documentation doesn't have any description. What's the purpose of this method and how to use it?
By running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring. Rails tests can also simulate browser requests and thus you can test your application's response without having to test it through your browser.
In a normal situation, when creating a new Rails app, you would run the rails new application_name command after installing Rails. Once the skeleton of the new Rails application has been built, you will see a folder called test in the root of the new application, and many subfolders and files within the test directory.
While unit tests make sure that individual parts of your application work, integration tests are used to test that different parts of your application work together.
Don't write view tests. You should be able to change copy or HTML classes without breaking your tests. Just assess critical view elements as part of your in-browser integration tests.
Be aware assigns
deprecated in Rails 5. And extracted to separate gem. To use it you must include 'rails-controller-testing' to your gemfile.
It means if a controller defined an instance variable @item="something"
.
You can fetch an instance variable in your test with e.g.:
# It will check if the instance variable is a string. assert_kind_of String, assigns(:item)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With