-
-
Save adzap/3870306 to your computer and use it in GitHub Desktop.
Testing fail?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I have a class that delegates functionality to a couple of objects that it | |
# constructs. I want to test this in isolation. I want to make sure that the | |
# objects are constructed with the right arguments. I also want to make sure | |
# that the results from the objects are handled correctly. | |
# | |
# I'm finding it hard to structure the code and test in a way that isn't | |
# cumbersome. What's below works, but it feels like a lot of stubbing and setup | |
# for something the should be simpler. | |
# | |
# Anyone got a better approach for this? | |
class TopLevelValidator | |
class << self | |
attr_accessor :validator_a_class | |
attr_accessor :validator_b_class | |
end | |
self.validator_class_a = ValidatorA | |
self.validator_class_b = ValidatorB | |
def initialize(some_info) | |
@some_info = some_info | |
end | |
def valid? | |
validator_a = self.class.validator_a_class.new(@some_info) | |
validator_b = self.class.validator_b_class.new(@some_info) | |
validator_a.valid? && validator_b.valid? | |
end | |
end | |
describe TopLevelValidator do | |
let(:validator_a_class) { stub(:new => passing_validator) } | |
let(:validator_b_class) { stub(:new => passing_validator) } | |
let(:some_info) { stub } | |
let(:passing_validator) { stub(:valid? => true) } | |
let(:failing_validator) { stub(:valid? => false) } | |
before(:all) do | |
@original_validator_a_class = TopLevelValidator.validator_a_class | |
@original_validator_b_class = TopLevelValidator.validator_b_class | |
end | |
before do | |
# Have to do this each example for stub instance created each time | |
TopLevelValidtor.validator_class_a = validator_a_class | |
TopLevelValidtor.validator_class_a = validator_a_class | |
end | |
after(:all) do | |
TopLevelValidator.validator_a_class = @original_validator_a_class | |
TopLevelValidator.validator_b_class = @original_validator_b_class | |
end | |
subject do | |
described_class.new(some_info) | |
end | |
it "passes the right info to validator A" do | |
validator_a_class.should_receive(:new).with(some_info) | |
subject.valid? | |
end | |
it "is invalid if validator A says so" do | |
validator_a_class.stub(:new => failing_validator) | |
subject.should_not be_valid | |
end | |
it "passes the right info to validator B" do | |
validator_b_class.should_receive(:new).with(some_info) | |
subject.valid? | |
end | |
it "is invalid if validator B says so" do | |
validator_b_class.stub(:new => failing_validator) | |
subject.should_not be_valid | |
end | |
it "is valid if both validators say so" do | |
subject.should be_valid | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not a fan of the before(:all) and after(:all). Make a mistake with one of them, and some other test somewhere else can start failing. In a large codebase, that's very painful.
If I was going to go down this road, I'd make the validator classes optional arguments to the constructor.