修改多个测试的工厂属性的最佳方法?

时间:2013-10-07 17:38:21

标签: ruby-on-rails-3 rspec factory-bot factories

尝试使用rspec和工厂测试一些路由。在规范测试中多次修改现有工厂的最佳方法是什么?

require "spec_helper"

describe gameController do
  describe "routing" do

    game = FactoryGirl.create(:game)

    it "routes to #show" do
      get("/game/1").should route_to("game#show", :id => "1")
    end

    it "routes to #show" do
      # need to modify 1 param of the factory.. how best to do this?
      get("/game/1").should route_to("game#show", :id => "1")
    end

  end
end

1 个答案:

答案 0 :(得分:0)

你基本上有两种选择。如果您只是在测试之间修改单个参数,那么最简单的做法就是:

before(:each) do
  game = FactoryGirl.create(:game)
end

it "does something" do
  get("/game/1").should route_to("game#show", :id => "1")
end

it "does something else" do
  game.update_attributes(:param => "value")
  get("/game/1").should route_to("game#show", :id => "1")
end

否则,你可以设置一个工厂女孩序列并在每个规范中做一个新的FactoryGirl.create。