Behaviour of before hooks during configuration

18 views
Skip to first unread message

watcher...@gmail.com

unread,
Aug 21, 2015, 10:39:28 AM8/21/15
to rspec
Hello everybody.

I'm confused with the behaviour of before hooks and I have several questions:

1.
The term :suite is supposed to run the hook once before the first spec runs. I need to set the host for the bunch of my request tests:
RSpec.configure do |config|
    config
.before(:suite) do
         host
! "foo.example.com"
   
end
end
This raises undefined method host! error, however this works ok if I change :suite with :all Why? What is the correct way to once set the host for all the tests?

2. Resources, created in before(:each) block, are deleted automatically after tests:
RSpec.describe "Projects", :type => :request do
    describe
"GET /projects" do
        context
"common case" do
            before
do
               
@project = create(:project) # factory
               
get projects_path, nil, {Accept: 'application/json'}  
           
end

            it
"should return status ok" do
           
....
           
....
       
end
   
end
end
However, if I change before with before(:all) (no need to create the resource again / make request again in my case), record stays in database. Why?

gem versions:
rspec (2.99.0)
rspec-rails (3.0.2)
rspec-core (3.0.4, 2.99.2)
rails (4.1.1)


Xavier Shay

unread,
Aug 21, 2015, 10:04:32 PM8/21/15
to rs...@googlegroups.com
 
 
 
On Fri, Aug 21, 2015, at 10:34 PM, watcher...@gmail.com wrote:
Hello everybody.
 
I'm confused with the behaviour of before hooks and I have several questions:

1.
The term:suite is supposed to run the hook once before the first spec runs. I need to set the host for the bunch of my request tests:

RSpec.configure do|config|
    config
.before(:suite)do
         host
!"foo.example.com"
end
end

This raises undefined method host! error, however this works ok if I change :suite with :all Why? What is the correct way to once set the host for all the tests?
I'm guessing `host!` is provided by rspec-rails, in which case it's only available to specs tagged as a rails controller (and not to suite)
 
Since this is a cheap operation, I'd be inclined to do it in a before(:each) block. That way even if another test changes it will still behave correctly.
 
 
2. Resources, created in before(:each) block, are deleted automatically after tests:
RSpec.describe "Projects",:type =>:request do
    describe
"GET /projects"do
        context
"common case"do
            before
do
@project= create(:project)# factory
get projects_path,nil,{Accept:'application/json'}

end

            it
"should return status ok"do
....
....
end
end
end
However, if I change before with before(:all) (no need to create the resource again / make request again in my case), record stays in database. Why?
By default rspec-rails applies a feature called "transactional fixtures", that wraps each spec in a transaction. This *does not* include before(:all) hooks, which explains what you're seeing. I don't recommend using `before(:all)` for database related things. `before(:each)` should be fine.
 
I know that's not the most satisfying answer, but hopefully it helps.
 
Cheers,
Xavier
 
 
gem versions:
rspec (2.99.0)
rspec-rails (3.0.2)
rspec-core (3.0.4, 2.99.2)
rails (4.1.1)
 


--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
 
Reply all
Reply to author
Forward
0 new messages