I am working on code that I haven't written and trying to understand how RSpec works.
let(:course) { create :new_course }
before { assign :course, course }
I know that let
creates a memoized variable.
What does assign
do in this example?
assigns relates to the instance variables created within a controller action (and assigned to the view).
The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.
RSpec::Expectations provides a simple, readable API to express the expected outcomes in a code example. To express an expected outcome, wrap an object or block in expect , call to or to_not (aliased as not_to ) and pass it a matcher object: expect(order. total).
I use the database_cleaner gem to scrub my test database before each test runs, ensuring a clean slate and stable baseline every time. By default, RSpec will actually do this for you, running every test with a database transaction and then rolling back that transaction after it finishes.
You must be looking at a view spec. In an rspec-rails view spec, assign
assigns its second argument to the template instance variable named by the first argument. In your example, assign :course, course
sets @course
in the template to the value of course
.
More here: https://www.relishapp.com/rspec/rspec-rails/v/3-3/docs/view-specs/view-spec
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With