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