Ruby on rails 3 为什么控制器变量不是';在一次测试中不可用?
我试图在中学习MichaelHartl的RubyonRails教程,但在实践中做了一些更改,首先是一些变体和Test::Unit框架。在本教程中,使用了RSpec,而我试图坚持使用Test::Unit+Shoulda上下文 在第9章中,我被要求通过一些使用称为“controller”的var的功能测试,但我的测试不起作用,因为它们发现“controller”不存在。这就是我得到的: marcel@pua:~/desenvolument/Rails3Examples/ror\u教程$rake 测试:最近加载的套件 /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/rake\u test\u loader 开始F ===========================================================================================================================失败:测试:使用有效的登录名(电子邮件和密码)发布“创建” 应重定向到用户显示页面。(会话控制器测试) [test/functional/sessions\u controller\u test.rb:58]:至少需要1个 找到与“title”匹配的元素0。事实并非如此。 ===============================================================================================================================E =========================================================================================================错误:测试:使用有效的登录名(电子邮件和密码)发布“创建” 应该登录用户。(SessionControllerTest):名称错误: 未定义的局部变量或方法“controller” 这是我翻译的(转换为Test::Unit+Sholuda上下文)测试:Ruby on rails 3 为什么控制器变量不是';在一次测试中不可用?,ruby-on-rails-3,testing,Ruby On Rails 3,Testing,我试图在中学习MichaelHartl的RubyonRails教程,但在实践中做了一些更改,首先是一些变体和Test::Unit框架。在本教程中,使用了RSpec,而我试图坚持使用Test::Unit+Shoulda上下文 在第9章中,我被要求通过一些使用称为“controller”的var的功能测试,但我的测试不起作用,因为它们发现“controller”不存在。这就是我得到的: marcel@pua:~/desenvolument/Rails3Examples/ror\u教程$rake 测试
class SessionControllerTest@attr
断言_equal@user,controller.current_user
结束
应该“重定向到用户显示页面”吗
post:create,:session=>@attr
断言\u选择“标题”,/Show/
结束
结束
结束
结束
有人知道如何让我的测试工作吗?看看官方的Rails测试指南,我发现在功能测试中启用了一个名为@controller的实例变量。因此,Test::Unit版本应该是:
should "sign in the user" do
post :create, :session => @attr
assert_equal @user, @controller.current_user
end
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
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
should "sign in the user" do
post :create, :session => @attr
assert_equal @user, @controller.current_user
end