gpt4 book ai didi

ruby-on-rails-3 - 为什么 Controller 变量在测试中不可用?

转载 作者:行者123 更新时间:2023-11-28 21:33:40 28 4
gpt4 key购买 nike

我正在努力学习 Michael Hartl 在 http://ruby.railstutorial.org/chapters/sign-in-sign-out 中的 Ruby on Rails 教程,但对实践进行了一些更改,最重要的是,一些变体和 Test::Unit 框架。在本教程中,使用了 RSpec,而我试图坚持使用 Test::Unit + Shoulda-context。

在第 9 章中,我应该通过一些使用名为“controller”的 var 的功能测试,但我的测试不起作用,因为他们发现“controller”不存在。这是我得到的:

marcel@pua:~/Desenvolupament/Rails3Examples/ror_tutorial$ rake test:recent Loaded suite /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/rake_test_loader Started F =============================================================================== Failure: test: POST 'create' with valid signin (email and password) should redirect to the user show page. (SessionsControllerTest) [test/functional/sessions_controller_test.rb:58]: Expected at least 1 element matching "title", found 0. is not true. =============================================================================== E =============================================================================== Error: test: POST 'create' with valid signin (email and password) should sign in the user. (SessionsControllerTest): NameError: undefined local variable or method `controller' for

test/functional/sessions_controller_test.rb:53:in `block (3 levels) in <class:SessionsControllerTest>'

=============================================================================== Finished in 0.957865676 seconds. 7 tests, 6 assertions, 1 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications 0% passed 7.31 tests/s, 6.26 assertions/s rake aborted! Command failed with status (1): [/home/marcel/.rvm/rubies/ruby-1.9.2-p290/b...] Tasks: TOP => test:recent (See full trace by running task with --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

有人知道如何让我的测试工作吗?

最佳答案

查看官方 Rails 测试指南 http://guides.rubyonrails.org/testing.html ,我已经看到在功能测试中启用了一个名为@controller 的实例变量。所以,Test::Unit 版本应该是:

should "sign in the user" do
post :create, :session => @attr
assert_equal @user, @controller.current_user
end

关于ruby-on-rails-3 - 为什么 Controller 变量在测试中不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9152389/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com