How does a minimal rail testing system work?

I just started to learn testing with minitest , but it sounds very confusing to me. What does this statement mean?

 test 'should not save simulations without name' do post = Post.new assert !post.save, 'Saved the simulations without a name' end 

Publication model

 class Post < ActiveRecord::Base validates_presence_of :name , :type belongs_to :user before_create :check_validation validates :type, :inclusion => { :in => 1..10, :message => 'The row must be between 1 and 10' } private def check_validation(data, type) .... end 

What will be the minitest method for the following conditions:

  • check if the name , type parameter exists when creating a new object of the Post class. How to do post_test class?
  • This test method checks for the presence of name in Post ?
  • How to check check_Validation on a PostTest class?
  • How to check throw error if posts not owned by users?

I have already created the post.yml and user.yml . How to proceed. I tried to read the official ruby documentation, but this is not very useful.

Thanks at adnvance

Change 1

my yml file

 one: name: 'test' type: 3 user_id: 1 

I tried:

 test 'Post must have all parameters' do assert assigns(:one).valid? 

end

+5
source share
2 answers

check if name, parameter type exists when creating a new Post object class. How can I execute the post_test class?

Does this verification method check for a name in a message?

Grade. The statement verifies that the new Post instance is not being saved, but there are several reasons for this result - for example, there are no attributes :name and :type , a before_create filter that can capture the save operation.

If you want to test each of these validations individually, you need to start with a known valid Post entry and manipulate it to initiate each check. Usually I will have at least one minimum permissible entry set in my fixture file - in this case I will name it :mvp . Example:

 test 'MVP is valid' do assert posts(:mvp).valid?, 'Minimum valid Post is invalid' end test 'should not save simulations without name' do post = posts(:mvp) post.name = nil assert post.invalid?, 'Post is valid without a name' end ... 

For the most part, I find that testing simple checks (availability, prime numbers, etc.) clutters my tests and smells a bit like testing a framework instead of my own application code, so I often skip them.

How can I check check_Validation through the PostTest class?

check_validation marked as a private method, and in the general case, you do not want to directly test private methods. Nevertheless, you can make sure that the conditions that you set on the newly created models meet your expectations:

 test 'newly created Post has settings XYZ' do post = Post.new(...) assert post.save, 'Post could not be saved' # assertions to test #check_validations side effects here... end 

You do not include the body of your check_validation method, so I do not know what you mean here. However, with this name, I am inclined to believe that you are trying to do some kind of verification, which would be best left on a special verification method defined using the validate method. See the docs to see if this applies in your case.

How to check throw error if messages do not belong to users?

I would make this a confirmed condition for your Post class as follows:

 class Post belongs_to :user validates :user, presence: true end 

Hope this helps. I tried to answer your questions in such a way that you can continue your work, but if you are interested in learning how to work productively with Rails and Minitest, you will want to do more in-depth research on how it is used with Rails.

+1
source

This is a poorly written test, so I can understand why you are confused. I assume there is a companion test that tries to show that Post can be saved when it has a name.

 test 'should save simulations with name' do post = Post.new name: 'simulator McSimulation' assert post.save, 'Must save the simulations with a name' end test 'should not save simulations without name' do post = Post.new assert !post.save, 'Saved the simulations without a name' end 

What does this statement mean?

This is a testing method. Minitest has three basic concepts: test class, test method, and approval method.

Testing class

A test class is a class that inherits from Minitest::Test . He can directly inherit it:

 class PostTest < Minitest::Test end 

Or he can inherit from him indirectly

 class ActiveSupport::TestCase < Minitest::Test end class PostTest < ActiveSupport::TestCase end 

All the testing behavior that we use is accessible because we create test classes.

Test method

A testing method is a method starting with test_ , for example:

 class PostTest < ActiveSupport::TestCase def test_valid # do the testing here end end 

Rails adds some syntactic sugar so that test methods can be created by passing a string and a block instead of the test method:

 class PostTest < ActiveSupport::TestCase test "is valid" do # do the testing here end end 

Your application uses rail-specific syntax. All test methods in the test class are performed by Minitest.

Approval Method

The test class and test methods are how you structure the test, the approval method is how you actually perform the test. The easiest way to indicate that the code is behaving as expected is to pass a true assert value and an optional description to give directions if the value is always false. This is what your expression does. Post#save returns the value you deny, and then go to assert .

Minitest defines many more approval methods .

Test improvement

I said this is a poorly written test. What I mean? Well, firstly, a statement may fail for a number of reasons, so this test is not as specific as it should be. Here are some changes I would make:

1) The refute method is inverting assert , so I would use refute value instead of assert !value .

 test 'should not save simulations without name' do post = Post.new refute post.save, 'Saved the simulations without a name' end 

2) The save call actually sends data to the database, which is slow and should be avoided when possible. Use valid? instead of save .

 test 'must not be valid without name' do post = Post.new refute post.valid?, 'Must be invalid without a name' end 

3) Make sure that the reason the model is invalid is related to the reason for the test, in particular that it needs a name. There are several reasons why this model is not valid and will not be saved. Here we have to be more specific. The best way to specify this IMO is to verify that the model errors object contains the reason for the missing name.

 test 'must not be valid without name' do post = Post.new refute post.valid?, 'Must be invalid without a name' assert post.errors[:name].any?, 'Must be invalid for missing name' end 

Of course, we can make this even more specific by specifying the value returned by post.errors[:name] . Let the assert_includes approval method be used, which claims that the collection contains a specific value:

 test 'must not be valid without name' do post = Post.new refute post.valid?, 'Must be invalid without a name' assert_includes post.errors[:name], "can't be blank", 'Must be invalid for missing name' end 

Hope this helps.

+3
source

All Articles