我目前正在一个大型应用程序中从rails 2迁移到rails 3。在我们的功能规范中,我们有很多这样的东西:
@model = Factory :model
@child = Factory :child
Model.stub!(:find).and_return(@model)
Child.stub!(:find).and_return(@child)
...
@child.should_receive(:method).twice
主要的问题是,如果我让它进入DB并获得子对象的实际实例,那么real:方法会使测试过于复杂(需要两个大工厂)且速度缓慢。
在代码中,我们使用各种方法来获取项目:查找、动态查找器等
@model = Model.find(1)
@child = @model.children.find_by_name(name)
您建议如何将此逻辑移至rails 3?关于另一个抨击/嘲笑库,有什么建议吗?
通常情况下,您会在控制器规范中模拟模型:
Model.stub!(:find).and_return(mock_model('Model'))
Child.stub!(:find).and_return(mock_model('Child'))
然而,当您在rails 3应用程序的Gemfile中获得gem "rspec-rails", "~> 2.0"
时,标准rails scaffold生成器将使用rspec为您生成规范,因此运行rails generate scaffold MyResource
将为您生成一些示例规范。
以下是rails/rspec将为控制器规范生成的内容的一个注释较少的版本,因此我认为这应该被视为"rspec方式"。
describe AccountsController do
# Helper method that returns a mocked version of the account model.
def mock_account(stubs={})
(@mock_account ||= mock_model(Account).as_null_object).tap do |account|
account.stub(stubs) unless stubs.empty?
end
end
describe "GET index" do
it "assigns all accounts as @accounts" do
# Pass a block to stub to specify the return value
Account.stub(:all) { [mock_account] }
get :index
# Assertions are also made against the mock
assigns(:accounts).should eq([mock_account])
end
end
describe "GET show" do
it "assigns the requested account as @account" do
Account.stub(:find).with("37") { mock_account }
get :show, :id => "37"
assigns(:account).should be(mock_account)
end
end
describe "GET new" do
it "assigns a new account as @account" do
Account.stub(:new) { mock_account }
get :new
assigns(:account).should be(mock_account)
end
end
end