ruby-on-rails – Rails 3工厂与简单实例化
发布时间:2020-12-17 03:45:34 所属栏目:百科 来源:网络整理
导读:有人可以解释为什么工厂比测试期间的简单实例更有用吗?更清楚的是,我没有看到以下区别: before(:each) do @attr = { :name = "Example User",:email = "user@example.com",:password = "foobar",:password_confirmation = "foobar" }endit "should create
有人可以解释为什么工厂比测试期间的简单实例更有用吗?更清楚的是,我没有看到以下区别:
before(:each) do @attr = { :name => "Example User",:email => "user@example.com",:password => "foobar",:password_confirmation => "foobar" } end it "should create a new instance given valid attributes" do User.create!(@attr) end 还有这个 before(:each) do @user = Factory(:user) end 其中有以下工厂: Factory.define :user do |user| user.name "Michael Hartl" user.email "mhartl@example.com" user.password "foobar" user.password_confirmation "foobar" end 解决方法
因为它可以让您在一个地方拥有所有必需的变量和关联.
此外,您可以轻松创建存根或简单地提取属性而无需其他代码. 一旦你有几个测试文件,你想要保持你的代码干燥,那么兴趣会更清晰. 边注: 您应该使用’let’而不是每次创建实例变量 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |