【发布时间】:2012-02-27 11:19:52
【问题描述】:
我正在尝试遵循 http://ruby.railstutorial.org/chapters/sign-in-sign-out 中 Michael Hartl 的 Ruby on Rails 教程,但对实践进行了一些更改,尤其是一些变化和 Test::Unit 框架。在本教程中,使用了 RSpec,而我尝试坚持使用 Test::Unit + Shoulda-context。
在第 9 章中,我应该通过一些使用名为“控制器”的 var 的功能测试,但我的测试不起作用,因为他们发现“控制器”不存在。这是我得到的:
marcel@pua:~/Desenvolupament/Rails3Examples/ror_tutorial$ rake 测试:最近加载的套件 /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/rake_test_loader 开始 F ==================================================== ============================= 失败:测试:使用有效的登录名(电子邮件和密码)发布“创建” 应该重定向到用户显示页面。 (会话控制器测试) [test/functional/sessions_controller_test.rb:58]:预计至少 1 匹配“标题”的元素,发现 0。不正确。 ==================================================== ============================== E ==================================================== ============================= 错误:测试:使用有效登录名(电子邮件和密码)发布“创建” 应该登录用户。 (会话控制器测试):名称错误: 用于
的未定义局部变量或方法“控制器”test/functional/sessions_controller_test.rb:53:in `block (3 levels) in <class:SessionsControllerTest>'================================================ ================================= 0.957865676 秒内完成。 7 个测试,6 个断言,1 个失败,1 个 错误, 0 未决, 0 遗漏, 0 通知 0% 通过 7.31 次测试/秒,6.26 次断言/秒 rake 中止!命令失败,状态 (1):[/home/marcel/.rvm/rubies/ruby-1.9.2-p290/b...] 任务:TOP => test:recent(通过使用 --trace 运行任务查看完整跟踪)
这是原始 (RSpec) 测试:
describe SessionsController do
...
describe "POST 'create'" do
...
describe "with valid email and password" do
before(:each) do
@user = Factory(:user)
@attr = { :email => @user.email, :password => @user.password }
end
it "should sign the user in" do
post :create, :session => @attr
controller.current_user.should == @user
controller.should be_signed_in
end
it "should redirect to the user show page" do
post :create, :session => @attr
response.should redirect_to(user_path(@user))
end
end
end
end
这是我翻译的(转换为 Test::Unit + Sholuda-context)测试:
class SessionsControllerTest < ActionController::TestCase
context "POST 'create'" do
context "with valid signin (email and password)" do
setup do
@attr = {email: "test@email.tst", password: "testpwd"}
@user=User.create! @attr.merge!({name: "test_user", password_confirmation: "testpwd"})
end
should "sign in the user" do
post :create, :session => @attr
assert_equal @user, controller.current_user
end
should "redirect to the user show page" do
post :create, :session => @attr
assert_select "title", /Show/
end
end
end
end
有人知道如何进行我的测试吗?
【问题讨论】: