5/3/2023 0 Comments Rspec timecop![]() This helps to increase readably of specs. I have tried to group all related specs for person’s voting eligibility in same describe block. Here I am using timecop gem to set certain dates in the system while running specs. new(birth_date: Date.parse('')) expect( person).to receive(:age).and_return(13) eligible = person.eligible_for_voting? expect( eligible).to be_falsey end end end end new(birth_date: Date.parse('')) expect( person).to receive(:age).and_return(23) eligible = person.eligible_for_voting? expect( eligible).to be_truthy end end it ' should return false when age is below 18' do eeze('') do person = Person. new(birth_date: Date.parse('')) expect( person).to receive(:age).and_return(18) eligible = person.eligible_for_voting? expect( eligible).to be_truthy end end it ' should return true when age is above 18' do eeze('') do person = Person. If anything goes wrong in the age method, that feedback will be provided by the unit test of age method.įor Person class your spec file person_spec.rb will look something like below: require 'rails_helper' describe Person, type: :model do describe '# age' do it ' should calculate and return age of person' do # data setup, method call, assertion end end describe '# eligible_for_voting?' do it ' should return true when age is 18' do eeze('') do person = Person. Use stubbed age in further logic and test functionality written in eligible_for_voting? method only. In this case you don’t need to test logic of calculating age in the specs of this method. Mocks/Exception part described above is not required in this spec as age method is not calling any other method of same or different class.Įligible_for_voting? method is using age method internally. You will be writing a separate unit test for age method to assert if its calculating age correctly. Another method eligible_for_voting? figures out eligibility of person for voting using age. age method calculates age of person using birth_date. Person class have two methods - age and eligible_for_voting?. Lets assume that you have following code class person.rb: class Person = AGE_ELLIGIBALE_FOR_VOATING end def age # Logic to calculate age end end When you have conditional branching in your method its good to group tests together as this will make it easy to read test and add new test when you update your method to accommodate new requirements. Incase method is returning value or setting attributes in database then assertion is required. This is not required when method is not returning any value. Assertion - Assertion are used to verify behaviour of method.Method may print some output, update attributes in database or return a value. ![]() Method call - At this point actual method which you want to test gets called.You can set multiple exceptions in a single test if multiple methods are getting called. If you want to ensure that another method is getting called along with mocking the output then you can set exception for the method call. When you simply want to mock output of the another method then you can use stubs. If method is calling another method inside it then that method call should be stubbed, as separate unit test will be taking care of testing behaviour of another method. Mocks/Exceptions - In unit test user intended to test behaviour of given method.In case you need to create file for execution of test make sure you delete the file after execution of test. Each test should create its required data and after execution of test it should also delete in. Data setup - Each test need some data to be used as method parameters or make decisions or call actual method that needs to be tested.To test a method or a brach of code following attributes are required: 1. When it has a conditional branching then, its good to write a separate test for each branching. A unit test is written to test a behaviour of a single method. Rspec is commonly used testing framework for writing unit test of Rails/Ruby applications. Testing instance methods with RSpec Rails
0 Comments
Leave a Reply. |