博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rspec 安装、使用、入门
阅读量:5099 次
发布时间:2019-06-13

本文共 4251 字,大约阅读时间需要 14 分钟。

INSTALL=======$ gem install rspecRSPEC-RAILS===========  RAILS-3  =======    CONFIGURE THE GEMFILE    ======================    group :development, :test do      gem "rspec-rails", "~> 2.0"    end    INSTALL THE BUNDLE    ===============================    $ bundle install    BOOTSTRAP THE APP    =================     $ ./script/rails generate rspec:install          create  .rspec          create  spec          create  spec/spec_helper.rb          create  autotest          create  autotest/discover.rb  RAILS-2  =======    INSTALL    =======    $ gem install rspec-rails -v 1.3.3    BOOTSTRAP THE APP    =================    $ ./script/generate rspec        create  spec        create  spec/spec_helper.rb        create  spec/spec.opts        create  previous_failures.txt        create  script/spec_server        create  script/spec
HOW TO USE==========  COMMAND LINE  =============rspec --color --format doc spec/widget_spec.rb  RAILS 3 (RSPEC 2)  =============./rails/generate model Userrake -T spec # lists all rspec rake tasksrake spec # run all specsrake spec/models/mymodel_spec.rb # run a single spec filerake spec/models/mymodel_spec.rb:27 # run a single example or group on line 27  RAILS 2 (RSPEC 1)  =============./script/generate rspec_model Userrake -T spec # lists all rspec rake tasksrake spec # run all specsrake spec SPEC=spec/models/mymodel_spec.rb SPEC_OPTS="-e \"should dosomething\"" #run a single specmodule UserSpecHelper  def valid_user_attributes    { :email => "joe@bloggs.com",      :username => "joebloggs",      :password => "abcdefg"}  endenddescribe "A User (in general)" do  include UserSpecHelper  before(:each) do    @user = User.new  end  it "should be invalid without a username" do    pending "some other thing we depend on"    @user.attributes = valid_user_attributes.except(:username)    @user.should_not be_valid    @user.should have(1).error_on(:username)    @user.errors.on(:username).should == "is required"    @user.username = "someusername"    @user.should be_valid  endendEXPECTATIONS=====================target.should satisfy {|arg| ...}target.should_not satisfy {|arg| ...}target.should equal 
target.should not_equal
target.should be_close
,
target.should_not be_close
,
target.should be
target.should_not be
target.should predicate [optional args]target.should be_predicate [optional args]target.should_not predicate [optional args]target.should_not be_predicate [optional args]target.should be < 6target.should == 5target.should be_between(1, 10)target.should_not == 'Samantha'target.should match
target.should_not match
target.should be_an_instance_of
target.should_not be_an_instance_of
target.should be_a_kind_of
target.should_not be_a_kind_of
target.should respond_to
target.should_not respond_to
lambda {a_call}.should raise_errorlambda {a_call}.should raise_error(
[, message])lambda {a_call}.should_not raise_errorlambda {a_call}.should_not raise_error(
[, message])proc.should throw
proc.should_not throw
target.should include
target.should_not include target.should have(
).thingstarget.should have_at_least(
).thingstarget.should have_at_most(
).thingstarget.should have(
).errors_on(:field)expect { thing.approve! }.to change(thing, :status). from(Status::AWAITING_APPROVAL). to(Status::APPROVED)expect { thing.destroy }.to change(Thing, :count).by(-1)Mocks and Stubs===============user_mock = mock "User"user_mock.should_receive(:authenticate).with("password").and_return(true)user_mock.should_receive(:coffee).exactly(3).times.and_return(:americano)user_mock.should_receive(:coffee).exactly(5).times.and_raise(NotEnoughCoffeeExcepion)people_stub = mock "people"people_stub.stub!(:each).and_yield(mock_user)people_stub.stub!(:bad_method).and_raise(RuntimeError)user_stub = mock_model("User", :id => 23, :username => "pat", :email =>"pat@example.com")my_instance.stub!(:msg).and_return(value)MyClass.stub!(:msg).and_return(value)Examples (in the real world)============================来源
http://madhatted.com/2008/7/10/rspec-real-world-testingpresentation: http://kerryb.github.com/iprug-rspec-presentation/#31

转载于:https://www.cnblogs.com/ToDoToTry/archive/2011/08/06/2129616.html

你可能感兴趣的文章
HDU 5510 Bazinga KMP
查看>>
[13年迁移]Firefox下margin-top问题
查看>>
Zookeeper常用命令 (转)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
在centos上开关tomcat
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
查询消除重复行
查看>>
[leetcode]Minimum Path Sum
查看>>
内存管理 浅析 内存管理/内存优化技巧
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
csv HTTP简单表服务器
查看>>
IO流写出到本地 D盘demoIO.txt 文本中
查看>>
Screening technology proved cost effective deal
查看>>
mysql8.0.13下载与安装图文教程
查看>>