0

尝试使用 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
4

1 回答 1

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。

于 2013-10-11T14:59:30.017 回答