I have a simple controller test, containing a.o. the following code:
context "POST :create" do
before (:each) do
post :create, :user_id => @user.id,
:account => { .. some data ... }
end
it { response.status.should == 201 }
it { response.location.should be_present }
end
Now I thought of a very simple way to speed up this test, and to use a before(:all)
instead of a before(:each)
. In that case the post would only be done once.
So i wrote:
context "POST :create" do
before (:all) do
post :create, :user_id => @user.id,
:account => { .. some data ... }
end
it { response.status.should == 201 }
it { response.location.should be_present }
end
But then I get the following errors:
RuntimeError:
@routes is nil: make sure you set it in your test's setup method.
Is this by design? Is there a way to circumvent it?
RSpec has the same goals as Minitest but focuses on readable specifications describing how the application is supposed to behave with a close match to English. Minitest purports that if you know Ruby well, it should be enough. The RSpec project focuses on behavior-driven development (BDD) and specification writing.
RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the "behavior" of an application being tested.
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.
Capybara is a framework which allows us to test the code in our views by filling in form data, inspecting page content and sending HTTP requests to the browser. Let's set up a spec to test our newly created form: rails generate rspec:feature form. This will generate the file spec/features/form_spec.rb .
I asked this question on the rspec mailing list, and got the following reply from @dchelimsky himself:
Yes. rspec-rails wraps the rails' testing framework which doesn't have a before(:all) concept in it, so all the data is reset before each example. Even if we wanted to support this in rspec-rails (which I don't) it would require changes to rails first.
So doing controller calls is not possible in a before(:all)
, it can only be used to setup your DB or instance variables.
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