rspec测试驱动

添加gem

group :development, :test do
    gem 'rspec-rails', '2.13.1'
end

要安装和包含这些新加的gem,运行bundle update和bundle install

bundle update
bundle install

设置Rails使用RSpec而不是Test::Unit。通过rails generate rspec:install命令实现

rails generate rspec:install

生成集成测试(request spec)

rails generate integration_test static_page

测试代码

    describe "Home page" do

        it "should have the content 'Demo App'" do
            visit '/static_pages/home'
            expect(page).to have_content('Demo App')
        end
    end

即是当访问地址为/static_pages/home的首页时,其内容应该包含"Demo App"这两个词。

把Capybara DSL加入RSpec帮助文件spec/spec_helper.rb

RSpec.configure do |config|
    .
    .
    .
    RSpec.configure do |config|
end

执行测试

bundle exec rspec spec/requests/static_pages_spec.rb  简写为 rspec spec

 

简化RSpec测试代码

    subject { page }

    describe "Home page" do
        before { visit root_path }

        it { should have_content('Demo App') }
        it { should have_title(full_title('')) }
        it { should_not have_title('| Home') }
    end

 

posted @ 2014-11-28 17:18  klfjsdkljfkjekgje  阅读(52)  评论(0)    收藏  举报