How to discover why RSpec is taking so long to start?
For anyone who comes here, the problem was that the RSpec was truncating every time I ran the tests.
I discovered it by checking the log while it was starting (tail -f log/test.log
).
To solve that, I used the database_cleaner gem and configured it with :transaction
as the clean strategy.
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
RSpec documentation...where is it hiding?
Not sure why you feel rdoc.info isn't useful - here's the matcher docs there:
http://rubydoc.info/gems/rspec-expectations/2.0.1/RSpec/Matchers
It may be that you're looking in the wrong spot; these were in the RSpec::Expectations gem, which is split out from the core in Rspec 2.
Controller action not running in rspec test
expect(controller).to receive(:index)
You've just stubbed your controller action. None of the code in your controller action is now running. Remove this line.
Why is this rspec request spec not updating the model?
you are fooled by how smart testing frameworks look :)
surely you expect the db entry for user_to_edit
to change. user_to_edit
is a local variable so user_to_edit.last_name
will not change no matter what buttons you click. try with { user_to_edit.reload.last_name }
Using shoulda to refactor rspec tests on Rails models
1) The Shoulda::Matchers::ActiveRecord module has a lot more in it than just column and index matchers. I would dig around in the included classes a little and see what you can find. This is where the have_many
, belong_to
etc come from. For the record though, I see little value in most of what is in there.
2) Yes, macros such as have_many
test a lot more than whether or not a model responds to a method. From the source code, you can see exactly what it is testing:
def matches?(subject)
@subject = subject
association_exists? &&
macro_correct? &&
foreign_key_exists? &&
through_association_valid? &&
dependent_correct? &&
class_name_correct? &&
order_correct? &&
conditions_correct? &&
join_table_exists? &&
validate_correct?
end
3) Making the tests more readable and/or concise is definitely a subjective question to answer. Everyone will give you a different answer to this depending on their background and experience. I would personally get rid of all of the respond_to
tests and replace them with tests that have value. When someone looks at your tests, they should be able to understand the public API for that class. When I see that your objects respond_to something like "following?", I can make assumptions, but don't really know what it means. Does it take an argument? Does it return a boolean value? Is the object following something or is something following the object?
Rspec & Factory Girl: Create one record to use before all describe blocks in a Model test
Use let
:
RSpec.describe Movie, type: :model do
let(:movie) { build(:movie) }
describe 'an awesome movie' do
it "is awesome" do
expect(movie.awesome?).to be true
end
end
end
The let
block is executed in the context of the example, so methods defined by let
are available in the test. This is also true of before
:
before do
movie.awesome = true
end
Statements in before
are executed before the examples within the describe block. Note that before
and let
can also work with a nested describe
, in which case they are defined for any context nested within the describe
.
Related Topics
Are There "Rules" for Ruby Syntactic Sugar
"Ruby.Exe Is Not Recognized as an Internal or External Command" in Windows 7
Date Range Facets with Sunspot in Ruby on Rails
Running a Shell Command from Ruby: Capturing the Output While Displaying the Output
How to Get Rufus-Scheduler Working with a Rails App Deployed to Heroku
How to Make a Custom Environment in Rails a Default Environment
Text_Field_With_Auto_Complete Inside Form_For
Invalid Configuration or No Rubies Listed
Error Nomethoderror: Undefined Method 'Debug_Rjs=' for Actionview::Base:Class
Os X Mavericks Install Rvm Warning
Heroku Rails Rake Task to Sync Production & Local Db
How to Make Block Local Variables the Default in Ruby 1.9
Killing Process Group from Ruby Kills My Whole Computer
How to Import Using Fastercsv a Row with a Name Like "CiaráN"
Which Http Web Server How to Use to Debug Ruby Code Using Rubymine 3.0.1