Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 Rspec应在没有lambda的情况下更改计数_Ruby On Rails 3_Rspec_Shoulda - Fatal编程技术网

Ruby on rails 3 Rspec应在没有lambda的情况下更改计数

Ruby on rails 3 Rspec应在没有lambda的情况下更改计数,ruby-on-rails-3,rspec,shoulda,Ruby On Rails 3,Rspec,Shoulda,我正试图找到另一种编写应该更改计数测试的方法(没有lambda)。 我使用的是Rails3。我也在使用shoulda matcher宝石 原因-所有测试用例的格式都是 describe "some stuff" do it { should ... } end 但是我不能按照相同的模式来测试应该更改计数 这是我的 describe "some stuff" do it "should change count by one" do lambda { ... }

我正试图找到另一种编写应该更改计数测试的方法(没有lambda)。 我使用的是Rails3。我也在使用shoulda matcher宝石

原因-所有测试用例的格式都是

describe "some stuff" do
   it { should ... }
 end
但是我不能按照相同的模式来测试应该更改计数

这是我的

describe "some stuff" do
    it "should change count by one" do 
        lambda { ... }.should change(Model, :count).by(1)
    end 
end
有办法写吗

describe "some stuff" do
   it { should change(Model, :count).by(1) }
 end

非常感谢

您还可以使用expect语法:

subject { lambda { ... } }

it { should change(Model, :count).by(1) }
describe "some stuff" do
  expect { ... }.to change(Model, :count).by(1)
end

我曾经写过这样的规范,实际上我试图找到一种更简洁的方法来实现这一点。这是最好的选择吗?