Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of assigns method in Rails tests (MiniTest)?

Tags:

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?

like image 937
Dan Avatar asked Nov 30 '14 14:11

Dan


People also ask

What does rails test do?

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.

How do you create a test case in rails?

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.

What are integration tests rails?

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.

What type of test is not typical in a rails application?

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.


2 Answers

Be aware assigns deprecated in Rails 5. And extracted to separate gem. To use it you must include 'rails-controller-testing' to your gemfile.

like image 21
woto Avatar answered Sep 19 '22 22:09

woto


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) 
like image 143
Anil Maurya Avatar answered Sep 22 '22 22:09

Anil Maurya