I'm using scaffolding to generate rspec controller tests. By default, it creates the test as:
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
describe "PUT update" do
describe "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested doctor" do
company = Company.create! valid_attributes
put :update, {:id => company.to_param, :company => new_attributes}, valid_session
company.reload
skip("Add assertions for updated state")
end
Using FactoryGirl, I've filled this in with:
let(:valid_attributes) { FactoryGirl.build(:company).attributes.symbolize_keys }
describe "PUT update" do
describe "with valid params" do
let(:new_attributes) { FactoryGirl.build(:company, name: 'New Name').attributes.symbolize_keys }
it "updates the requested company", focus: true do
company = Company.create! valid_attributes
put :update, {:id => company.to_param, :company => new_attributes}, valid_session
company.reload
expect(assigns(:company).attributes.symbolize_keys[:name]).to eq(new_attributes[:name])
This works, but it seems like I should be able to test all attributes, instead of just testing the changed name. I tried changing the last line to:
class Hash
def delete_mutable_attributes
self.delete_if { |k, v| %w[id created_at updated_at].member?(k) }
end
end
expect(assigns(:company).attributes.delete_mutable_attributes.symbolize_keys).to eq(new_attributes)
That almost worked, but I'm getting the following error from rspec having to do with BigDecimal fields:
-:latitude => #<BigDecimal:7fe376b430c8,'0.8137713195 830835E2',27(27)>,
-:longitude => #<BigDecimal:7fe376b43078,'-0.1270954650 1027958E3',27(27)>,
+:latitude => #<BigDecimal:7fe3767eadb8,'0.8137713195 830835E2',27(27)>,
+:longitude => #<BigDecimal:7fe3767ead40,'-0.1270954650 1027958E3',27(27)>,
Using rspec, factory_girl, and scaffolding is incredibly common, so my questions are:
What is a good example of an rspec and factory_girl test for a PUT update with valid params?
Is it necessary to use attributes.symbolize_keys
and to delete the mutable keys? How can I get those BigDecimal objects to evaluate as eq
?
This is the questioner posting. I had to go down the rabbit hole a bit in understanding multiple, overlapping issues here, so I just wanted to report back on the solution I found.
tldr; It's too much trouble trying to confirm that every important attribute comes back unchanged from a PUT. Just check that the changed attribute is what you expect.
The issues I encountered:
(Factory.build :company).attributes.symbolize_keys
, which winds up creating new problems.Date.inspect
does not show milliseconds.Here's the Hash method, which could go in rails_spec.rb:
Alternatively (and perhaps preferably) I could have written a custom rspec matcher than iterates through each attribute and compares their values individually, which would have worked around the date issue. That was the approach of the
assert_records_values
method at the bottom of the answer I selected by @Benjamin_Sinclaire (for which, thank you).However, I decided instead to go back to the much, much simpler approach of sticking with
attributes_for
and just comparing the attribute I changed. Specifically:I hope this post allows others to avoid repeating my investigations.
Well, I did something that's quite simpler, I'm using Fabricator, but I'm pretty sure it's the same with FactoryGirl:
Also, I'm not sure why you are building a new factory, PUT verb is supposed to add new stuff, right?. And what you are testing if what you added in the first place (
new_attributes
), happens to exist after theput
in the same model.Ok so this is how I do, I don't pretend to strictly follow the best practices, but I focus on precision of my tests, clarity of my code, and fast execution of my suite.
So let take example of a
UserController
1- I do not use FactoryGirl to define the attributes to post to my controller, because I want to keep control of those attributes. FactoryGirl is useful to create record, but you always should set manually the data involved in the operation you are testing, it's better for readability and consistency.
In this regard we will manually define the posted attributes
2- Then I define the attributes I expect for the updated record, it can be an exact copy of the posted attributes, but it can be that the controller do some extra work and we also want to test that. So let's say for our example that once our user updated his personal information our controller automatically add a
need_admin_validation
flagThat's also where you can add assertion for attribute that must remain unchanged. Example with the field
age
, but it can be anything3- I define the action, in a
let
block. Together with the previous 2let
I find it makes my specs very readable. And it also make easy to write shared_examples4- (from that point everything is in shared example and custom rspec matchers in my projects) Time to create the original record, for that we can use FactoryGirl
As you can see we manually set the value for
age
as we want to verify it did not change during theupdate
action. Also, even if the factory already set the age to 25 I always overwrite it so my test won't break if I change the factory.Second thing to note: here we use
let!
with a bang. That is because sometimes you may want to test your controller's fail action, and the best way to do that is to stubvalid?
and return false. Once you stubvalid?
you can't create records for the same class anymore, thereforlet!
with a bang would create the record before the stub ofvalid?
5- The assertions itself (and finally the answer to your question)
Summarize So adding all the above, this is how the spec looks like
assert_record_values
is the helper that will make your rspec simpler.As you can see with this simple helper when we expect for a
BigDecimal
, we can just write the following, and the helper do the restSo at the end, and to conclude, when you have written your shared_examples, helpers, and custom matchers, you can keep your specs super DRY. As soon as you start repeating the same thing in your controllers specs find how you can refactor this. It may take time at first, but when its done you can write the tests for a whole controller in few minutes
And a last word (I can't stop, I love Rspec) here is how my full helper look like. It is usable for anything in fact, not just models.
This code can be used to solve your two issues:
It solves the problem of comparing floating point numbers by converting the values to it string representation using JSON.
It also solves the problem of checking that the new values have been updated but the rest of the attributes have not changed.
In my experience, though, as the complexity grows, the usual thing to do is to check some specific object state instead of "expecting that the attributes I don't update won't change". Imagine, for instance, having some other attributes changing as the update is done in the controller, like "remaining items", "some status attributes"... You would like to check the specific expected changes, that may be more than the updated attributes.
Here is my way of testing PUT. That is a snippet from my
notes_controller_spec
, the main idea should be clear (tell me if not):Instead of
FactoryGirl.build(:company).attributes.symbolize_keys
, I'd writeFactoryGirl.attributes_for(:company)
. It is shorter and contains only parameters that you specified in your factory.Unfortunately that is all I can say about your questions.
P.S. Though if you lay BigDecimal equality check on database layer by writing in style like
this may work for you.
Testing the rails application with rspec-rails gem. Created the scaffold of user. Now you need to pass all the examples for the user_controller_spec.rb
This has already written by the scaffold generator. Just implement
Now will pass many examples from this file.
For invalid_attributes be sure to add the validations on any of field and
In the users model .. validation for first_name is as =>
Now all the examples created by the generators will pass for this controller_spec