The Last Day Of Summer

.NET技术 C# ASP.net ActiveReport SICP 代码生成 报表应用 RDLC
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Ruby on rails开发从头来(windows)(二十二)-测试Controller

Posted on 2007-11-12 12:50  Cure  阅读(1763)  评论(0编辑  收藏  举报

上次测试Modeul的问题还没有解决,但是下面的还要继续,这次来测试Controller

1.       test\functional目录下,rails已经为我们的controller生成了对应的测试文件,要注意application_controller不会生成测试文件。我们以控制登录的LoginController为例,打开login_controller_test.rb,内容如下:

require File.dirname(__FILE__) + '/../test_helper'

require 'login_controller'

 

# Re-raise errors caught by the controller.

class LoginController; def rescue_action(e) raise e end; end

 

class LoginControllerTest < Test::Unit::TestCase

def setup

@controller = LoginController.new

@request    = ActionController::TestRequest.new

@response   = ActionController::TestResponse.new

end

# Replace this with your real tests.

def test_truth

assert true

end

end

我们看到,在setup方法里,定义了三个对象@controller@request@response,这样,我们就可以在不接入webservernetwork的情况下进行测试了。

2.       我们来把其中的test_truth方法替换成下面的代码:

def test_index

get :index

assert_response :success

end

其中,get方法模拟发出一个web请求,请求的actionindex,并且捕捉响应(response),然后由assert_response断言来判断响应是否成功。

现在运行测试:depot>ruby test/functional/login_controller_test.rb

会看到测试失败了,命令行的输出:

Expected response to be a <:success>, but was <302>

1 tests, 1 assertions, 1 failures, 0 errors

为什么会这样呢?回想一下,我们在前面的程序里,在访问index页面时,要先判断用户是不是管理员。如果不是,就要跳转到login页面,在login_controller.rb里:

before_filter :authorize, :except => :login

application.rb文件里,有authorize方法:

def authorize

unless session[:user_id]

flash[:notice] = "Please log in"

redirect_to(:controller => "login", :action => "login")

end

end

3.       好了,知道了原因,现在再写一个测试:

def test_index_without_user

get :index

assert_redirected_to :action => "login"

assert_equal "Please log in", flash[:notice]

end

上面的代码里,我们尝试请求index这个action,并且使用断言判断是否重定向到login,再判断flash[:notice]里的内容。

再次运行测试,命令行的输出如下:

Loaded suite test/functional/login_controller_test

Started

.

Finished in 0.062 seconds.

1 tests, 3 assertions, 0 failures, 0 errors

可以看到所有的断言都通过了。

但是还有一个问题,就是根据代码,我们只使用了两个断言,但是提示信息却显示有三个断言,这个问题是什么原因还不太清楚,但是现在并不影响我们继续下面的内容。

4.       在我们的login页面上,用户输入名字和密码后,点击login按钮,这些信息将会发送个login这个action,然后创建一个User的实例来让用户登入。

login_controller.rb里的login方法里:

@user = User.new(params[:user])

logged_in_user = @user.try_to_login

现在,我们来测试login这个action,在login_controller_test.rb中添加方法:

def test_login_with_invalid_user

post :login, :user => {:name => 'fred', :password => 'opensesame'}

assert_response :success

assert_equal "Invalid user/password combination", flash[:notice]

end

大家一定看到了,这是使用了一个不存在的用户名和密码来测试login

运行测试,输出结果如下:

Finished in 0.609 seconds.

2 tests, 5 assertions, 0 failures, 0 errors

和上一个测试方法相对的,这里代码里是两个断言,根据输出也是两个,挺奇怪的事情。

5.       测试了不存在的用户,现在我们来测试用户存在的情况。

修改fixtures目录下的users.yml文件,我们还使用上面的用户名和密码:

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

fred:

  id: 1

  name: fred

  hashed_password: <%= Digest::SHA1.hexdigest('abracadabra') %>

然后给login_controller_test.rb添加fixtures :users,指定使用users.yml文件。

再添加方法:

def test_login_with_valid_user

post :login, :user => {:name => 'fred', :password => 'abracadabra'}

assert_redirected_to :action => "index"

assert_not_nil(session[:user_id])

user = User.find(session[:user_id])

assert_equal 'fred', user.name

end

在这里方法里,我们指定了和yml文件中定义的用户名相一致的数据,并且判断是否定位到index,再判断用户的id是否已经保存在session中。

根据前面测试Model时候的内容,我们在yml文件中定义的数据会被加入到数据库中,这样,这个方法里的三个断言都应该通过才对。

OK了,运行测试,输出结果:

Finished in 0.125 seconds.

3 tests, 8 assertions, 0 failures, 0 errors

嗯,所有的断言都通过了。

 

好了,这次就到这里。