[rspec-users] RSpec Testing ActiveRecord config dependency.

David Chelimsky dchelimsky at gmail.com
Mon Feb 9 13:11:46 EST 2009


On Mon, Feb 9, 2009 at 9:12 AM, James Byrne <lists at ruby-forum.com> wrote:
> I have the following library code:
>
>    def normal_time_now
>      return DateTime.now.utc if default_timezone == :utc
>      return DateTime.now
>    end
>
> This is dependent upon a setting in config/environment.rb
>
>  # Make Active Record use UTC-base instead of local time
>  config.active_record.default_timezone = :utc
>
> I want to test that I get the expected results with the config set to
> utc and otherwise.  The library code is used to automatically set a
> model attribute on create.  My existing specification looks somewhat
> like this:
>
> describe "Builds a Model with custom magic columns" do
>  before(:all) do
>    build_model   :magiks do
>      string    :description
>    # these are Rails' own magic columns
> ...
>    # these are our custom magic columns
> ...
>    end
>    @my_mage = Magik.new
>    @my_mage.save!
>  end
>
>  it "should set each custom magic column present" do
>    (Magik.column_names & ActiveRecord::Base::HLL_AUDIT_COLUMNS).each do
> |magic_column|
>      @my_mage.read_attribute(magic_column).should_not be_nil
>    end
>  end
>
> I can simply create duplicate separate specification file and set the
> configuration value appropriately in each. Something along the lines of:
>
> describe "Builds a Model with custom magic columns" do
>  before(:all) do
>  config.active_record.default_timezone = nil
>    or
>  config.active_record.default_timezone = :utc
>
> But this strikes me as inappropriate.  I believe that this test belongs
> inside the basic specification file but I cannot conceive of how to do
> this.  Basically, I require some form of variable setup routine. Does
> anyone have any suggestions on how to handle this case?

I'd keep it isolated. You know that
config.active_record.default_timezone = :utc works, right? So you
don't need to specify that. You're specifying that when
default_timezone == :utc, it behaves one way, but otherwise it behaves
differently. So:

magik = Magik.create
def magic.default_timezone; :utc; end
....

OR

magik = Magik.create
magik.stub!(:default_timezone).and_return(:utc)

Now this violates a principle that you shouldn't stub things on the
object you're spec'ing, but the alternative is to play with global
values, which violates other principles. This is the simplest way IMO.
But if you're concerned about internals of AR changing, you could do
this:

describe Thing do
  before(:each) do
    @original_default_timezone = Thing.default_timezone
  end
  it "does something" do
    Thing.default_timezone = :abc
    thing = Thing.new
    thing.default_timezone.should == :abc
  end
  before(:each) do
    Thing.default_timezone = @original_default_timezone
  end
end

That will restore things after each example.

HTH,
David

>
> Regards,
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list