####expect{} vs expect()
I don't really understand procs.
####RSpec tagging Use tags to run only specific tests.
Command line -- bundle exec rspec --tag focus spec/features/tags_spec.rb
Test -- note the :focus => true
name:value pair. See documentation for details.
scenario "delete a tag", :focus => true do
visit tags_path
click_link "SimpleForm"
click_button "Delete Tag"
expect(current_path).to eq tags_path
expect(page).to_not have_content("SimpleForm")
end
####Testing boolean values Test
describe Tag do
it "is invalid without a Name" do
invalid_params = { name: nil }
tag = Tag.create(name: "RSpec")
expect(tag.update(invalid_params)).to be false
end
end
Output
Failures:
1) Tag is invalid without a Name
Failure/Error: expect(tag.update(invalid_params)).to be false
expected #<FalseClass:0> => false
got #<TrueClass:20> => true
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
# ./spec/models/tag_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.02042 seconds
1 example, 1 failure
####Testing default controller rendering Is this kind of a pointless test since this is the default rails behavior?
describe "GET #index" do
it "renders the :index template" do
get :index
expect(response).to render_template :index
end
end
On the other hand, here I'm redirecting to #index so testing this may make more sense.
describe "PATCH #update" do
context "with valid attributes" do
it "redirects to the :index template" do
patch :update, { id: @tag.id, tag: { name: "rspec" } }
expect(response).to redirect_to tags_path
end
end
end
####Clean out DB?
Did a puts "#{params}"
in one of my controllers during a test I was running and saw this {"id"=>"444", "controller"=>"tags", "action"=>"update"}
. Should I be cleaning out the DB in some way so that every time I run tests, id
is back at 1?
####assigns() method requires instance variable in controller Test
context "with valid attributes" do
it "finds the correct Tag from the DB" do
patch :update, id: @tag, name: @tag
expect(assigns(:tag)).to eq(@tag)
end
end
TagsController#update
def update
@tag = Tag.find(params[:id])
@tag.update(params[:name])
redirect_to tags_path, notice: "Tag updated"
end
If I used tag
instead of @tag
in my controller, the specs fail, expecting a Tag object and getting nil instead. I believe the reason for this is that assigns()
method looks at the instance variable available to the view. In this case tag
is not available to the view and so I see the nil result. This correct?
####update() method
update()
takes a key - value hash. If you pass it a key such as @tag.update(tag_params[:name]) you're going to get a NoMethodError something like
undefined method stringify_keys' for "RSpec"
. stringify_keys is method that takes a hash and converts all the keys to strings, see documentation.