Tag: rspec2

是否存在匹配哈希的RSpec2匹配器?

未来读者请注意:认为RSpec不认为您的哈希值相等? 一个可能是OrderedHash,但是从常规RSpec输出你无法分辨。 这是提示这篇文章的问题。 原始问题: 假设我有一个规范,我想测试一个方法生成适当的哈希。 it ‘should generate the Hash correctly’ do expected = {:foo => 1, ‘baz’ => 2} subject.some_method_that_should_generate_the_hash.should == expected end 这通常会失败,因为具有相同键值对的不同哈希可能会以不同的顺序返回它们的对。 结果如下: Failure/Error: subject.some_method_that_should_generate_the_hash.should == expected expected: {:foo => 1, ‘baz’ => 2}, got: {‘baz’ => 2, :foo => 1} 对于数组,使用=〜运算符求解。 但是,这对Hashes不起作用。 现在,我已经诉诸了 it ‘should generate the Hash correctly’ do expected […]

在Ruby中重置单例实例

如何在Ruby中重置单个对象? 我知道在实际代码中我们永远不想这样做但是unit testing呢? 这是我在RSpec测试中尝试做的事情 – describe MySingleton, “#not_initialised” do it “raises an exception” do expect {MySingleton.get_something}.to raise_error(RuntimeError) end end 它失败了,因为我之前的一个测试初始化​​了单例对象。 我试过从这个链接开始关注Ian White的建议,它基本上是猴子补丁Singleton来提供reset_instance方法,但我得到一个未定义的方法’reset_instance’exception。 require ‘singleton’ class <<Singleton def included_with_reset(klass) included_without_reset(klass) class <<klass def reset_instance Singleton.send :__init__, self self end end end alias_method :included_without_reset, :included alias_method :included, :included_with_reset end describe MySingleton, "#not_initialised" do it "raises an exception" […]

是否有针对ruby 1.9和RSpec2的良好突变测试工具?

我曾经使用过Heckle,但由于ParseTree的问题,它与ruby 1.9不兼容。 我一直在寻找替代方案,但唯一看起来很有希望的是Chaser,而且我没有任何明确的文档可以用来看看我是否可以使用RSpec。 它似乎有Test :: Unit依赖。 那么 – 是否有人使用任何很酷的工具来真正检查测试的质量? 或者 – 有没有提供比c0覆盖更好的覆盖工具? 这有助于解决同样的问题。 我现在正在使用cover_me,但它是c0,就像rcov一样。

RSpec:如何编写unit testing用例来接收在私有方法中引发的exception

我已经为Race Condition实施了乐观锁定。 为此,我在Product中添加了一个额外的列lock_version 。 方法: recalculate是调用private method_1然后保存( save! )产品。 我不能用save! 在私有method_1 ,因为它将失败其他东西。 我不想重构业务逻辑。 #Product: Model’s new field: # lock_version :integer(4) default(0), not null def recalculate method_1 self.save! end private def method_1 begin #### #### if self.lock_version == Product.find(self.id).lock_version Product.where(:id => self.id).update_all(attributes) else raise ActiveRecord::StaleObjectError.new(self, “test”) end rescue ActiveRecord::StaleObjectError => e if tries < 3 tries […]

如何在使用JRuby或bundle exec时使用rspec启用颜色?

我正试图用JRuby运行我的rspec: rake spec 这导致: jruby -S bundle exec rspec –color spec/foo_spec.rb 没有颜色出现,所以我从等式中删除了Jruby: bundle exec rspec –color spec/foo_spec.rb 没有颜色。 如何将“–color”选项传递给rspec? 我在项目的根目录中也有一个.rspec文件,在这些情况下似乎没有帮助。 但是,当我运行时,会提取或使用.rspec文件: rspec spec/foo_spec.rb 有任何想法吗?

RunTimeError:ActionController :: RackDelegation在rspec 2.10.1中用于rails 3.1.4应用程序控制器

在我们的rails 3.1.4 app中, rspec用于测试应用程序控制器中的公共方法require_signin 。 这是方法require_signin: def require_signin if !signed_in? flash.now.alert = “Log in first!” redirect_to signin_path end end 这是rspec代码: it “should invoke require_signin for those without login” do controller.send(:require_signin) controller {should redirect_to signin_path} end 以上rspec生成巨大的多页错误,如下所示: RuntimeError:←[0m ←[31mActionController::RackDelegation#status= delegated to @_response.status=, but @_response is nil: #”text/html”}, @_status=200, @_reques t=#[1, 1], “rack.input”=>#, …….. rspec代码有什么问题? 非常感谢。

Rails / RSpec:如何测试#initialize方法?

如何使用RSpec指定#initialize行为? 例如这里: generator.rb class Generator attr_accessor :seed def initialize(seed = nil) @seed = seed || pick_seed end def pick_seed Time.now.to_i end end generator_spec.rb require ‘generator’ describe Generator it “calls ‘pick_seed’ method unless seed specified” do end end 我想设置期望从#initialize方法调用#initialize方法。

Rails:良好的Rspec2示例用法? (另外:Cucumber,Pickle,Capybara)

我正在寻找一个使用Rspec 2作为测试库的最新开源应用程序。 我想看看有经验的开发人员如何正确地利用库来测试完整的堆栈,因为我对自己的知识一直存在疑问(来自testunit,部分原因是由于最新Rspec版本的相当稀疏的文档,甚至虽然它不断改进)。 如果一个项目使用Cucumber,Pickle和/或Capybara以及Rspec 2,你会让我高兴得跳起来。 有什么指针吗? 干杯!

Rspec:期望与预期阻止 – 有什么区别?

刚学习rspec语法,我注意到这段代码有效: context “given a bad list of players” do let(:bad_players) { {} } it “fails to create given a bad player list” do expect{ Team.new(“Random”, bad_players) }.to raise_error end end 但是这段代码没有: context “given a bad list of players” do let(:bad_players) { {} } it “fails to create given a bad player list” do expect( Team.new(“Random”, […]