1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Test If An Instance Variable Was Assigned as a rails til

This commit is contained in:
jbranchaud
2019-11-26 14:15:51 -06:00
parent 0200c64804
commit e1dbaeb428
2 changed files with 26 additions and 1 deletions

View File

@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
_876 TILs and counting..._ _877 TILs and counting..._
--- ---
@@ -551,6 +551,7 @@ _876 TILs and counting..._
- [Show Pending Migrations](rails/show-pending-migrations.md) - [Show Pending Migrations](rails/show-pending-migrations.md)
- [Show Rails Models With Pry](rails/show-rails-models-with-pry.md) - [Show Rails Models With Pry](rails/show-rails-models-with-pry.md)
- [Show Rails Routes With Pry](rails/show-rails-routes-with-pry.md) - [Show Rails Routes With Pry](rails/show-rails-routes-with-pry.md)
- [Test If An Instance Variable Assigned](rails/test-if-an-instance-variable-was-assigned.md)
- [Truncate Almost All Tables](rails/truncate-almost-all-tables.md) - [Truncate Almost All Tables](rails/truncate-almost-all-tables.md)
- [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md) - [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md)
- [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md) - [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md)

View File

@@ -0,0 +1,24 @@
# Test If An Instance Variable Was Assigned
When testing Rails controller actions, you'll often be making assertions about
the response to your test request. You may also want to assert about the
instance variables being set, as those are headed for your view layer.
For instance, if an instance variable `@metadata` is supposed to be set in a
`#show` controller action, you can assert that it is with [RSpec's
`assigns`](https://relishapp.com/rspec/rspec-rails/docs/controller-specs):
```ruby
describe "when given valid params" do
it "sets the metadata" do
get :show, params: valid_params
expect(assigns(:metadata)).to match(
identifier: "abc123",
session_id: "fe98f08c-bf2f-4749-9f81-071d9cc7720e",
)
end
end
```
[source](https://stackoverflow.com/questions/2051373/how-to-test-instance-variable-was-instantiated-in-controller-with-rspec)