【问题标题】:Best way to modify factory attributes for multiple tests?为多个测试修改工厂属性的最佳方法?
【发布时间】:2013-10-07 17:38:21
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你基本上有两个选择。如果您只是在测试之间修改单个参数,则最简单的方法可能是执行以下操作:

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      相关资源
      最近更新 更多