I have a test suite structured as follows:
describe ... do
[list of dates].each do
describe
before(:all) do
base_date = ...
end
describe ... do
[list of times].each do
describe ... do
before(:all) do
base_time = base_date + ...
DateTime.stub!(:now).and_return(base_time)
end
describe ... do
<test using records within date-time range based on base_time>
end
describe ... do
<another test using records within date-time range based on base_time>
end
end
end
end
end
end
end
The first test has DateTime(now) == base_time, but the second test as DateTime(now) == my computer's date-time, indicating that the stub is no longer in effect. Moving the stub! call into each describe loop resolves the problem, but I would like to understand why it doesn't work as written.
The reason lies probably elsewhere, stubs work fine with multiple nested describe blocks. Maybe :all vs :each is the problem: before(:all) is executed once before all describe blocks are executed, while before(:each) is executed each time before a describe block is executed.
Or maybe it has something to do with stubbing DateTime, have you tried
DateTime.any_instance.stub(:now).and_return(base_time)
Related
I'm writing rspec tests like so
describe Module do
describe "method" do
context "first_context" do
before do
stub_const("Module::CONST", "stub0")
end
# logic
end
context "second_context" do
before do
stub_const("Module::CONST", "stub0 stub1")
end
# logic
end
end
end
and about 75% of the time the tests pass as the stub_const logic is working, but 25% of the time a race condition fails and the stub_const from the first test flows in to the const for the second test, so the second test's Module::CONST value is "stub0". Why is this happening?
I've seen this sort of thing happen on JRuby. You can try adding explicit locking around any code that stubs globals, or running each of the examples under a lock:
$lock = Mutex.new
around do |example|
$lock.synchronize do
example.run
end
end
Ensure this is before your before hooks.
I have this code that I want to reuse in several specs:
RSpec.shared_context "a UserWorker" do |user|
let(:mock_context_user) {{
id: 1,
brand: user.brand,
backend_token: user.backend_token
}}
before(:each) do
allow(SomeClass).to receive(:some_method)
.with(user.id).and_return(mock_context_user)
end
before(:each, context: true) do
Sidekiq::Testing.inline!
end
after(:each, context: true) do
Sidekiq::Testing.fake!
end
end
And in the spec file that uses the shared code:
let(:user) { build :user } # FactoryGirl
...
describe '#perform' do
# some lets here
include_context 'a UserWorker', user
context 'when something exists' do
it 'does some stuff' do
# test some stuff here
end
end
end
But that gives me this error:
/.rvm/gems/ruby-2.3.0#fb-cont/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:724:in `method_missing': `user` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). (RSpec::Core::ExampleGroup::WrongScopeError)
Suggestions? Any help is appreciated.
The RSpec docs aren't very clear on this, but you can inject additional values by passing a block containing let() calls to include_context. The "customization block" passed by the spec will be evaluated first, and is available to the code declared in the shared context.
Here's a shared context that depends on the specs including it to let() a value, value_from_spec, and then sets a couple more values, one via let() and one via a before() block:
RSpec.shared_context('a context', shared_context: :metadata) do
# assume the existence of value_from_spec
let(:a_value_from_context) { value_from_spec - 1 }
before(:each) do
# assume the existence of value_from_spec
#another_value_from_context = value_from_spec + 1
end
end
(Note that unlike the OP's |user| example, we never explicitly declare value_from_spec, we just trust that it'll be there when we need it. If you want to make what's going on more obvious, you could check defined?(:value_from_spec) and raise an error.)
And here's a spec that injects that value, and reads the shared context's transformations of it:
describe 'passing values to shared context with let()' do
# "customization block"
include_context 'a context' do
# set value_from_spec here
let(:value_from_spec) { 1 }
end
describe 'the context' do
it 'should read the passed value in a let() block' do
expect(a_value_from_context).to eq(0)
end
it 'should read the passed value in a before() block' do
expect(#another_value_from_context).to eq(2)
end
end
end
Since it will always return the same mock_context_user, you can try something more generic like:
allow(SomeClass)
.to receive(:some_method)
.with(an_instance_of(Fixnum))
.and_return(mock_context_user)
But I'm not actually sure if an_instance_of is available for RSpec 3.5, it is on RSpec 3.3.
In order to avoid repeating myself a lot in my Rspec tests, I'd like to write a function like this
def with_each_partner(&block)
PARTNER_LIST.each do |partner|
context "with partner #{partner.name}" { yield partner }
end
end
I have such a function and it works in the sense that all the tests run with the correct value for partner supplied, but they don't print during output as being part of the "with partner X" context: instead if I have a test like this:
describe Thing do
subject { Thing.new(partner) }
with_each_partner do |partner|
it 'does its thing' do
expect(subject.do).to eq 'its thing'
end
end
end
I end up with output like this:
Thing
does its thing
Instead of the desired output, which is like:
Thing
with partner X
does its thing
with partner Y
does its thing
How can I get RSpec to correctly work with the context created in my function?
TL;DR: do this:
def with_each_partner(&block)
PARTNER_LIST.each do |partner|
context "with partner #{partner.name}" do
class_exec(&block)
end
end
end
Explanation
RSpec's DSL works by evaluating the blocks with a changed self -- this is how it is a method within a describe or context block, but not outside of it. When you yield, the provided block is evaluated with the original self that was self at the point the block was defined. What that means is that with your original with_each_partner definition, this code:
describe Thing do
subject { Thing.new(partner) }
with_each_partner do |partner|
it 'does its thing' do
expect(subject.do).to eq 'its thing'
end
end
end
Is really being evaluated like this:
describe Thing do
subject { Thing.new(partner) }
outer_example_group = self
with_each_partner do |partner|
outer_example_group.it 'does its thing' do
expect(subject.do).to eq 'its thing'
end
end
end
...and so the individual examples are defined on the outer example group, not on the "with partner #{partner.name}" nested group.
class_exec evaluates the provided block in the context of the class/module. In this case, the class is the example group subclass that RSpec has generated for your context. Using class_exec ensures that when it is called, the receiver is your nested context example group rather than the outer example group, creating the result you want.
I have a test that looks like this:
class PageTest < ActiveSupport::TestCase
describe "test" do
test "should not save without attributes" do
page = Page.new
assert !page.save
end
end
end
When running the tests, I get 0 tests, 0 assertions. If I remove the describe "test" do, I get the 1 test, 1 assertions. So I have the feeling that the describe "..." do is actually making the test disappear.
What is going on here? What am I missing?
Looks like you're mixing up minitest specs and ActiveSupport::TestCase. If you check the rails guides on testing the test method is explained but it's not used with describe.
Rails adds a test method that takes a test name and a block. It
generates a normal MiniTest::Unit test with method names prefixed with
test_. So,
test "the truth" do
assert true
end
acts as if you had written
def test_the_truth
assert true
end
The describe syntax is explained in the minitest docs under the spec section and is used with it (and not test). Like so:
describe "when asked about cheeseburgers" do
it "must respond positively" do
#meme.i_can_has_cheezburger?.must_equal "OHAI!"
end
end
Hello I am newbie of Rails and now trying to make a simple tweeting application.
I wrote a simple test and tried to use before(:all) because I don't want to visit page each time, but the test doesn't keep the visit page object.
Off course when I use before(:each) the test success, but when it increase to a large number, I suppose the time of test increase, too.
How can I write this test with before(:all)? Thanks for your kindness.
# /spec/requests/tweet_pages.spec
require 'spec_helper'
describe "TweetPages" do
describe "GET /tweet_pages" do
before(:each) {visit tweets_path} # this line pass test but...
#before(:all) {visit tweets_path} # next line fails in second it test.
subject {page}
context do
its(:status_code) {should == 200}
it {should have_selector 'input'}
end
end
end
If I'm not mistaken before(:all) runs for all examples within a given describe or context block. In this case, your examples are in their own context block. If you would remove the context block the tests should pass. Otherwise you could add the before(:all) block to the describe or context block you want the before(:all) to have access to.
N.B. It is also recommended to add an after(:all) block to save yourself from the trouble before(:all) can give you. In general, using before(:all) isn't really recommended.
before(:each) runs once for each it statement.
before(:all) runs once for each context or describe block that it's in.
Trying to force before(:all) in your case would be counter-productive, but one approach would be to store the result in a ##class_variable and later reuse it in your subject.