第一行result应该等于5;
第二行result应该包含5;
第三行result应该响应hello;
第四行lambda匿名函数应该抛出错误信息“Nothing find!”;
第五行result应该匹配正则表达式;
第六行期望代码块能把bug状态从“open”改为“fixed”;
第七行result应该为空;
Should() 方法表示肯定,还有should_not()方法表示否定,该方法的工作机制是先执行should或者should_not后边的代码,返回一个 match对象,进而把此match对象作为参数传给should()或者should_not()方法,和拥有对象句柄的本地match对象作对比校 验,若匹配不成功,返回相应信息
测试controller
classHomeController<ApplicationController def login
@person=Person.where(name: params[:person][:name])if@person.blank?
redirect_to root_path
else
redirect_to people_path
end endend
describe HomeControllerdo
render_views
it "Logs in Person with non-blank name"do
person =Factory(:Person, name:"non-blank name")
get :login
response.should redirect_to(people_path)end
it "does not log in Person with blank name"do
person =Factory(:Person, name:"")# blank name
get :login
response.should redirect_to(root_path)endend
处理文字编码与相应的引用
describe
"admin/roles/index"
do
before(
:each
)
do
@ability
=
Object
.
new
@ability
.extend(CanCan::Ability)
controller.stub(
:current_ability
) {
@ability
}
view.stub(
:current_ability
) {
@ability
}
assign(
:roles
, Role.paginate(
:page
=> params[
:page
],
:per_page
=>
10
).order(
"updated_at DESC"
))
end
it "has a create link for that role inside admin" do @ability.can :create, Role render if Role.count > 0 rendered.should have_selector("div") do |new_a| new_a.should have_selector "a", :href => new_admin_role_path, :content => "添加" end end end
it "has a destroy link for that role inside admin" do
@ability.can :destroy, Role
render
if Role.count > 0
rendered.should have_selector("tr td") do |destroy_a|
destroy_a.should have_selector "a",
:href => admin_role_path(Role.first),
:"data-method" => "delete",
:"data-confirm" => "删除之后,该角色下的用户将无该角色下的权限。确定删除吗?",
:rel => "nofollow",
:content => "删除"
end
end
end
it "has a update link for that role inside admin" do @ability.can :update, Role render if Role.count > 0 rendered.should have_selector("tr td") do |update_a| update_a.should have_selector "a", :href => "/admin/roles/"+Role.first.id+"/edit", :content => "修改" end end end
it "renders a list of admin/roles" do role = Role.first @ability.can :destroy, Role @ability.can :update, Role render # Run the generator again with the --webrat flag if you want to use webrat matchers allow_message_expectations_on_nil assigns[:roles].stub!(:total_pages).and_return(1) assert_select "tr>td", :text => role.name.to_s, :count => 1 assert_select "tr td", :text => role.description.to_s, :count => 1 mycount = Role.count assert_select "a", :html => "修改",:count => mycount < 10?mycount:10 assert_select "a", :html => "删除", :count => mycount < 10?mycount:10 end end