Reputation: 1123
I have a class called db_conn.rb
in which I have 2-methods
1. One method is to check if db connection is established or not which is called is_conn?
2. Another one is to close db connection i.e., close_conn
.
Now I want to write rspec
for close_conn
. My logic is to run close_conn
and then call is_conn?
to check the boolean value which should be false
.
Below is my spec file. I need more guidance in achieving this.
describe DdModule::DbConnn do
before(:context) {
puts "Testing DB connection..."
@obj = DbModule::DbConn.new("hostname", "instance", "port", "user", "pass")
}
it "connect_db constructor takes five parameters and returns true if connection establishes" do
expect(@obj.is_conn?).to eq true
end
it "connect_db close_connection should close the connection and is_connection should return false" do
@obj.close_conn
expect(@db_obj.is_conn?).to eq false
end
I am seeing the following output:
rspec
Testing DB connection...
Oracle Connection user@jdbc:oracle:thin:@host:1521/instance initialized..
.FFF
Failures:
1) DbModule::DbConn close_connection should close the connection and is_conn should return false
Failure/Error: expect(@obj.is_conn?).to eq false
expected: false
got: true
(compared using ==)
# ./spec/conn_db_spec.rb:21:in `(root)'
Finished in 0.2 seconds (files took 0.27 seconds to load)
2 examples, 1 failures
Upvotes: 1
Views: 153
Reputation: 4639
If it's an instance method that you expect to change with another instance method, you need to write your expectation like this (i made a test class locally to ensure it works).
expect {@obj.close_conn}.to change(@obj, :is_conn?).from(true).to(false)
If it were me, I would write the full spec like this
describe DdModule::DbConnn do
let!(:db_conn) do
DbModule::DbConn.new("hostname", "instance", "port", "user", "pass")
end
describe '#close_conn' do
it 'closes the db connection' do
expect {db_conn.close_conn}.to change(db_conn, :is_conn?).from(true).to(false)
end
end
end
Upvotes: 1