我正试图在http://ruby.railstutorial.org/chapters/sign-in-sign-out中关注Michael Hartl的Ruby on Rails教程,但是在实践中有一些变化,首先是一些变化和Test :: Unit框架。在本教程中,使用了RSpec,而我正在尝试坚持Test :: Unit + Shoulda-context。
在第9章中,我倾向于传递一些使用名为'controller'的var的功能测试,但我的测试不起作用,因为他们发现'controller'不存在。这就是我得到的:
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. ================================================== =============================失败:测试:使用有效登录(电子邮件和密码)POST'创建' 应该重定向到用户显示页面。 (SessionsControllerTest) [test / functional / sessions_controller_test.rb:58]:预计至少为1 元素匹配“title”,找到0.不是真的。 ================================================== ============================= E. ================================================== =============================错误:测试:使用有效登录(电子邮件和密码)POST'创建' 应该登录用户。 (SessionsControllerTest):NameError:
的未定义局部变量或方法`controller'
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断言/ s耙中止!命令失败,状态为(1):[/ home / marcel / .rvm / rubies / ruby-1.9.2-p290 / b ...]任务:TOP =&GT; 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
有人知道如何让我的测试工作吗?
答案 0 :(得分:1)
查看http://guides.rubyonrails.org/testing.html上的官方Rails测试指南,我看到在功能测试中启用了一个名为@controller的实例变量。所以,Test :: Unit版本应该是:
should "sign in the user" do
post :create, :session => @attr
assert_equal @user, @controller.current_user
end