背景:我遇到了Thoughtbot的"Factory Girl"宝石的一些问题,用于创建在单位和其他测试中使用的对象.我想去控制台并运行不同的Factory Girl电话来查看发生了什么.例如,我想进去那里做...
>> Factory(:user).inspect
Run Code Online (Sandbox Code Playgroud)
我知道你可以在不同的环境中运行控制台......
$ script/console RAILS_ENV = test
但是当我这样做时,工厂课程不可用.看起来好像test_helper.rb
没有加载.
我尝试了各种require
调用,包括一个绝对路径,test_helper.rb
但它们与此类似地失败:
$ script/console RAILS_ENV=test
>> require '/Users/ethan/project/contactdb/test/test_helper.rb'
Errno::ENOENT: No such file or directory -
/Users/ethan/project/contactdb/config/environments/RAILS_ENV=test.rb
Run Code Online (Sandbox Code Playgroud)
格儿.哎呀.
在我的代码中,我使用了Shoulda匹配器进行了以下验证,效果很好:
it { should validate_presence_of(:name) }
Run Code Online (Sandbox Code Playgroud)
在我的模型中,我已将条件添加到我的验证中:
validates_presence_of :name, :if => eligible?
Run Code Online (Sandbox Code Playgroud)
是否有可能在验证中反映出来?
我已经尝试过查看shoulda匹配器的文档,但无法找到解决方案.
非常感谢!
我的ActiveRecord中有以下验证.
validates :active, :inclusion => {:in => ['Y', 'N']}
Run Code Online (Sandbox Code Playgroud)
我使用以下来测试我的模型验证.
should_not allow_value('A').for(:active)
should allow_value('Y').for(:active)
should allow_value('N').for(:active)
Run Code Online (Sandbox Code Playgroud)
是否有更清洁,更通过测试方式?我目前正在使用RSpec2和shoulda匹配器.
编辑
经过一些环顾四周我才发现,这可能是一种'好'的测试方式,如果没有为此提供任何东西,任何需要它的人都可以为它编写自己的自定义匹配器.(并可能将其贡献给项目) .可能有趣的讨论的一些链接:
我正在设置一个rails应用程序,我刚刚完成了一些单元测试,我的朋友说显然固定装置不再很酷,人们现在正在使用RSpec或者应该.我想知道使用这些其他工具包的实际好处是什么.任何信息都是值得赞赏的.
-fREW
我正在尝试使用FactoryGirl
/ 来模拟会话shoulda
(它使用了固定装置,但我在使用工厂时遇到了问题).我有以下工厂(用户登录和电子邮件都有unique
验证):
Factory.define :user do |u|
u.login 'quentin'
u.email 'quentin@example.com'
end
Factory.define :session_user, :class => Session do |ses|
ses.association :user, :factory => :user
ses.session_id 'session_user'
end
Run Code Online (Sandbox Code Playgroud)
这是测试
class MessagesControllerTest < ActionController::TestCase
context "normal user" do
setup do
@request.session[:user_id]=Factory(:user).id
@request.session[:session_id]=Factory(:session_user).session_id
end
should "be able to access new message creation" do
get :new
assert_response :success
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时rake test:functionals
,我得到了这个测试结果
1) Error:
test: normal user should be able to access new message creation. (MessagesControllerTest): …
Run Code Online (Sandbox Code Playgroud) 我正在用Rspec测试我的ActiveRecord模型.我刚刚在我的一个验证中添加了一条自定义错误消息,如下所示:
validates :accepted_terms_at, :presence => {:message => 'You must accept the Terms and Conditions to use this site.'}
Run Code Online (Sandbox Code Playgroud)
现在,以下测试失败:
it { should validate_presence_of(:accepted_terms_at) }
Run Code Online (Sandbox Code Playgroud)
......有错误Expected errors to include "can't be blank" when accepted_terms_at is set to nil
.
因此测试失败,因为它正在查看验证错误消息并期望找到默认消息.
如何告诉Rspec新验证消息应该是什么?
1)消息作为参数:
it {should validate_presence_of(:accepted_terms_at, :message => 'your message')}
这给出了错误 wrong number of arguments (2 for 1)
2)作为链式方法调用的消息
it {should validate_presence_of(:accepted_terms_at).with('your message')}
这会引发错误,因为没有with
方法.
使用shoulda-matcher和RSpec的新期望语法的正确格式是什么?
使用Test :: Unit和Shoulda.试着去测试Users.create
.我的理解是Rails表单为这样的对象发送params:
user[email]
Run Code Online (Sandbox Code Playgroud)
在你的行动中变成哈希,对吧?
params[:user][:email]
Run Code Online (Sandbox Code Playgroud)
好的,所以在我的测试中我试过......
setup { post :create, :post => { 'user[email]' => 'invalid@abc' } }
Run Code Online (Sandbox Code Playgroud)
和
setup { post :create, :post => { :user => { :email => 'abc@abcd' } } }
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,在我的行动中,params[:user]
都是零.
我通常可以使用以下命令行语法为方法"delete_user_test"测试常规的Test :: Unit方法:
ruby functional/user_controller_test.rb -n delete_user_test
Run Code Online (Sandbox Code Playgroud)
现在,当我使用带有Test :: Unit的shoulda插件时,我尝试使用如下相同的技术:
...
context "Deleting a User" do
should "remove user from user table" do
...
end
end
Run Code Online (Sandbox Code Playgroud)
然后我尝试按如下方式运行单个测试:
ruby functional/user_controller_test.rb -n "test: Deleting a User should remove user from user table"
Run Code Online (Sandbox Code Playgroud)
这不起作用.有谁知道如何使用shoulda和Test :: Unit运行单个上下文测试.我在一个测试文件中有几个不同的测试,我想只使用TDD运行一个,而不必等待所有测试运行.
我知道你可以使用Shoulda轻松测试属于的关系:
describe Dog dog
it { should belong_to(:owner) }
end
Run Code Online (Sandbox Code Playgroud)
是否可以使用Shoulda测试更复杂的belongs_to关系?像这样的东西:
class Dog < ActiveRecord::Base
belongs_to :owner, :class_name => "Person", :foreign_key => "person_id"
end
Run Code Online (Sandbox Code Playgroud) shoulda ×10
ruby ×6
rspec ×4
testing ×3
unit-testing ×2
activerecord ×1
factory-bot ×1
rspec-rails ×1