rspec views

本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接: http://www.firedragonpzy.com.cn/index.php/archives/475


#coding:utf-8
require 'spec_helper'
require 'will_paginate/array'
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

你可能感兴趣的:(rspec)