Inside the employee model spec, define a structure to separate the tests for Validations, Attributes, Scopes and Instance Methods. Use either describe
or context
.
- Test that an employee can be valid when properly set up.
- Test that an employee is invalid without a
first_name
. - Test that an employee is invalid without a
last_name
- Test that an employee has exactly the expected attributes
- Test that the
zero_balance
scope returns expected results from the database. - Make sure that the database has at least one record in it that should not be returned by the
zero_balance
scope. - Make sure that the database has at least two records in it that should be returned by the
zero_balance
scope.
- Test that the
full_name
method returns expected results- Since we validate
first_name
andlast_name
, there is only one context to cover - Test that the
can_afford?
method returns expected results- Cover the affordable context and the unaffordable context
- Since we validate
The spec/requests/create_employee_spec.rb
needs to be completed by testing that the employee is successfully created and has the expected values and is returned as JSON from the endpoint.
- Write expectation on the status code.
- Write an expecation on the values submitted via the body of the request.
We want to write a spec for the RewardsReport#notify method. This method calls an external service that is expensive and we want to use a spy to prevent that call but still assert that it takes place. The value sent to the external service is based on the total_purchases
method which is built by summing all the initial rewards purchase_counts
. Use an array of reward doubles that stub their purchase_counts
in order to specify the total sent to the NotificationService.
- Instantiate the RewardsReport with an array of 3 reward doubles. Set the 3 doubles to have purchase counts of 30, 70, and 100 respectively.
- Write an isolated spec for the
.notify
instance method of RewardsReport. - Use a Spy to allow and then expect the method that is called and the value it is called with.
- Assert that the spy received the value 200 (the total of our doubles) as a parameter to the
send_purchase_report
method.