Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 330 lines (265 sloc) 11.051 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
require 'spec_helper'

describe FactoryGirl::Factory do
  before do
    @name = :user
    @class = define_class('User')
    @factory = FactoryGirl::Factory.new(@name)
    FactoryGirl.register_factory(@factory)
  end

  it "has a factory name" do
    @factory.name.should == @name
  end

  it "responds to factory_name" do
    @factory.factory_name.should == @name
  end

  it "has a build class" do
    @factory.build_class.should == @class
  end

  it "has a default strategy" do
    @factory.default_strategy.should == :create
  end

  it "passes a custom creation block" do
    proxy = stub("proxy", :result => nil, :add_observer => true)
    FactoryGirl::Proxy::Build.stubs(:new => proxy)
    block = lambda {}
    factory = FactoryGirl::Factory.new(:object)
    factory.to_create(&block)

    factory.run(FactoryGirl::Proxy::Build, {})

    proxy.should have_received(:result).with(instance_of(FactoryGirl::AttributeAssigner), block)
  end

  it "returns associations" do
    factory = FactoryGirl::Factory.new(:post)
    FactoryGirl.register_factory(FactoryGirl::Factory.new(:admin))
    factory.declare_attribute(FactoryGirl::Declaration::Association.new(:author, {}))
    factory.declare_attribute(FactoryGirl::Declaration::Association.new(:editor, {}))
    factory.declare_attribute(FactoryGirl::Declaration::Implicit.new(:admin, factory))
    factory.associations.each do |association|
      association.should be_association
    end
    factory.associations.size.should == 3
  end

  it "includes associations from the parent factory" do
    association_on_parent = FactoryGirl::Declaration::Association.new(:association_on_parent, {})
    association_on_child = FactoryGirl::Declaration::Association.new(:association_on_child, {})

    factory = FactoryGirl::Factory.new(:post)
    factory.declare_attribute(association_on_parent)
    FactoryGirl.register_factory(factory)

    child_factory = FactoryGirl::Factory.new(:child_post, :parent => :post)
    child_factory.declare_attribute(association_on_child)

    child_factory.associations.map(&:name).should == [:association_on_parent, :association_on_child]
  end

  describe "when overriding generated attributes with a hash" do
    before do
      @name = :name
      @value = 'The price is right!'
      @hash = { @name => @value }
    end

    it "returns the overridden value in the generated attributes" do
      declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
      @factory.declare_attribute(declaration)
      result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
      result[@name].should == @value
    end

    it "does not call a lazy attribute block for an overridden attribute" do
      declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, lambda { flunk })
      @factory.declare_attribute(declaration)
      @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
    end

    it "overrides a symbol parameter with a string parameter" do
      declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
      @factory.declare_attribute(declaration)
      @hash = { @name.to_s => @value }
      result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
      result[@name].should == @value
    end
  end

  describe "overriding an attribute with an alias" do
    before do
      @factory.declare_attribute(FactoryGirl::Declaration::Static.new(:test, 'original'))
      Factory.alias(/(.*)_alias/, '\1')
      @result = @factory.run(FactoryGirl::Proxy::AttributesFor,
                             :test_alias => 'new')
    end

    it "uses the passed in value for the alias" do
      @result[:test_alias].should == 'new'
    end

    it "discards the predefined value for the attribute" do
      @result[:test].should be_nil
    end
  end

  it "guesses the build class from the factory name" do
    @factory.build_class.should == User
  end

  it "creates a new factory using the class of the parent" do
    child = FactoryGirl::Factory.new(:child, :parent => @factory.name)
    child.compile
    child.build_class.should == @factory.build_class
  end

  it "creates a new factory while overriding the parent class" do
    child = FactoryGirl::Factory.new(:child, :class => String, :parent => @factory.name)
    child.compile
    child.build_class.should == String
  end
end

describe FactoryGirl::Factory, "when defined with a custom class" do
  subject { FactoryGirl::Factory.new(:author, :class => Float) }
  its(:build_class) { should == Float }
end

describe FactoryGirl::Factory, "when given a class that overrides #to_s" do
  let(:overriding_class) { Overriding::Class }

  before do
    define_class("Overriding")
    define_class("Overriding::Class") do
      def self.to_s
        "Overriding"
      end
    end
  end

  subject { FactoryGirl::Factory.new(:overriding_class, :class => Overriding::Class) }

  it "sets build_class correctly" do
    subject.build_class.should == overriding_class
  end
end

describe FactoryGirl::Factory, "when defined with a class instead of a name" do
  let(:factory_class) { ArgumentError }
  let(:name) { :argument_error }

  subject { FactoryGirl::Factory.new(factory_class) }

  its(:name) { should == name }
  its(:build_class) { should == factory_class }
end

describe FactoryGirl::Factory, "when defined with a custom class name" do
  subject { FactoryGirl::Factory.new(:author, :class => :argument_error) }
  its(:build_class) { should == ArgumentError }
end

describe FactoryGirl::Factory, "with a name ending in s" do
  let(:name) { :business }
  let(:business_class) { Business }

  before { define_class('Business') }
  subject { FactoryGirl::Factory.new(name) }

  its(:name) { should == name }
  its(:build_class) { should == business_class }
end

describe FactoryGirl::Factory, "with a string for a name" do
  let(:name) { :string }
  subject { FactoryGirl::Factory.new(name.to_s) }
  its(:name) { should == name }
end

describe FactoryGirl::Factory, "for namespaced class" do
  let(:name) { :settings }
  let(:settings_class) { Admin::Settings }

  before do
    define_class("Admin")
    define_class("Admin::Settings")
  end

  context "with a namespaced class with Namespace::Class syntax" do
    subject { FactoryGirl::Factory.new(name, :class => "Admin::Settings") }

    it "sets build_class correctly" do
      subject.build_class.should == settings_class
    end
  end

  context "with a namespaced class with namespace/class syntax" do
    subject { FactoryGirl::Factory.new(name, :class => "admin/settings") }

    it "sets build_class correctly" do
      subject.build_class.should == settings_class
    end
  end
end

describe FactoryGirl::Factory do
  let(:factory_with_non_existent_strategy) do
    FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) { }
  end

  let(:factory_with_stub_strategy) do
    FactoryGirl::Factory.new(:object, :default_strategy => :stub)
  end

  before do
    define_class("User")
    define_class("Admin", User)
    FactoryGirl.register_factory(factory_with_stub_strategy)
  end

  it "raises when trying to use a non-existent strategy" do
    expect { factory_with_non_existent_strategy }.to raise_error
  end

  it "creates a new factory with a specified default strategy" do
    factory_with_stub_strategy.default_strategy.should == :stub
  end

  describe "defining a child factory without setting default strategy" do
    subject { FactoryGirl::Factory.new(:other_object, :parent => factory_with_stub_strategy.name) }
    before { subject.compile }

    it "inherits default strategy from its parent" do
      subject.default_strategy.should == :stub
    end
  end

  describe "defining a child factory with a default strategy" do
    subject { FactoryGirl::Factory.new(:other_object, :default_strategy => :build, :parent => factory_with_stub_strategy.name) }
    before { subject.compile }

    it "overrides the default strategy from parent" do
      subject.default_strategy.should == :build
    end
  end
end

describe FactoryGirl::Factory, "human names" do
  context "factory name without underscores" do
    subject { FactoryGirl::Factory.new(:user) }
    its(:names) { should == [:user] }
    its(:human_names) { should == ["user"] }
  end

  context "factory name with underscores" do
    subject { FactoryGirl::Factory.new(:happy_user) }
    its(:names) { should == [:happy_user] }
    its(:human_names) { should == ["happy user"] }
  end

  context "factory name with big letters" do
    subject { FactoryGirl::Factory.new(:LoL) }
    its(:names) { should == [:LoL] }
    its(:human_names) { should == ["lol"] }
  end

  context "factory name with aliases" do
    subject { FactoryGirl::Factory.new(:happy_user, :aliases => [:gleeful_user, :person]) }
    its(:names) { should == [:happy_user, :gleeful_user, :person] }
    its(:human_names) { should == ["happy user", "gleeful user", "person"] }
  end
end

describe FactoryGirl::Factory, "running a factory" do
  subject { FactoryGirl::Factory.new(:user) }
  let(:attribute) { FactoryGirl::Attribute::Static.new(:name, "value", false) }
  let(:declaration) { FactoryGirl::Declaration::Static.new(:name, "value", false) }
  let(:proxy) { stub("proxy", :result => "result", :add_observer => true) }
  let(:attributes) { [attribute] }
  let(:attribute_list) { stub('attribute-list', :declarations => [declaration], :to_a => attributes) }

  before do
    define_model("User", :name => :string)
    FactoryGirl::Declaration::Static.stubs(:new => declaration)
    declaration.stubs(:to_attributes => attributes)
    FactoryGirl::Proxy::Build.stubs(:new => proxy)
    subject.declare_attribute(declaration)
  end

  it "creates the right proxy using the build class when running" do
    subject.run(FactoryGirl::Proxy::Build, {})
    FactoryGirl::Proxy::Build.should have_received(:new).once
  end

  it "returns the result from the proxy when running" do
    subject.run(FactoryGirl::Proxy::Build, {}).should == "result"
  end

  it "calls the block and returns the result" do
    block_run = nil
    block = lambda {|result| block_run = "changed" }
    subject.run(FactoryGirl::Proxy::Build, { }, &block)
    block_run.should == "changed"
  end
end

describe FactoryGirl::Factory, "#with_traits" do
  subject { FactoryGirl::Factory.new(:user) }
  let(:admin_trait) { FactoryGirl::Trait.new(:admin) }
  let(:female_trait) { FactoryGirl::Trait.new(:female) }

  before do
    FactoryGirl.register_trait(admin_trait)
    FactoryGirl.register_trait(female_trait)
  end

  it "returns a factory with the correct traits" do
    subject.with_traits([:admin, :female]).processing_order[1, 2].should == [admin_trait, female_trait]
  end

  it "doesn't modify the original factory's processing order" do
    subject.with_traits([:admin, :female])
    subject.processing_order.should == [subject.definition]
  end
end
Something went wrong with that request. Please try again.