Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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 查找上次创建的记录RSpec测试_Ruby On Rails_Ruby_Rspec_Rspec Rails - Fatal编程技术网

Ruby on rails 查找上次创建的记录RSpec测试

Ruby on rails 查找上次创建的记录RSpec测试,ruby-on-rails,ruby,rspec,rspec-rails,Ruby On Rails,Ruby,Rspec,Rspec Rails,如何编写测试来查找上次创建的记录 这是我要测试的代码: Post.order(在::desc.创建)。首先 如果您调用了方法“last_post”,我也在使用factorybot: def self.last_post Post.order(created_at: :desc).first end 然后在你的测试中: it 'should return the last post' do expect(Post.last_post).to eq(Post.last) end 另一方面

如何编写测试来查找上次创建的记录

这是我要测试的代码:

Post.order(在::desc.创建)。首先


如果您调用了方法“last_post”,我也在使用factorybot

def self.last_post
  Post.order(created_at: :desc).first
end
然后在你的测试中:

it 'should return the last post' do
  expect(Post.last_post).to eq(Post.last)
end
另一方面,编写代码的最简单方法是

Post.last
您不应该真正测试ruby方法的结果(您应该确保调用了正确的ruby方法),因此如果您这样做了:

def self.last_post
  Post.last
end
那么您的测试可能是:

it 'should send the last method to the post class' do
   expect(Post).to receive(:last)
   Post.last_post
end

您没有测试“last”方法调用的结果,只是测试它被调用。

如果您调用了“last\u post”方法:

def self.last_post
  Post.order(created_at: :desc).first
end
然后在你的测试中:

it 'should return the last post' do
  expect(Post.last_post).to eq(Post.last)
end
另一方面,编写代码的最简单方法是

Post.last
您不应该真正测试ruby方法的结果(您应该确保调用了正确的ruby方法),因此如果您这样做了:

def self.last_post
  Post.last
end
那么您的测试可能是:

it 'should send the last method to the post class' do
   expect(Post).to receive(:last)
   Post.last_post
end

您没有测试“最后一次”方法调用的结果-只是测试它被调用。

接受的答案不正确。只需执行
Post.last
即可按
ID
排序,而不是按创建时间排序

如果您使用的是顺序ID(理想情况下不应该这样),那么显然这会起作用,但如果不是,则需要指定排序依据的列。因此,要么:

def self.last_post
  order(created_at: :desc).first
end
或:

就我个人而言,我希望将其作为一个范围,而不是一个专门的方法

scope :last_created -> { order(:created_at).last }
这允许您使用其他作用域创建一些不错的链接,例如,如果您有一个特定用户/帐户查找所有帖子,那么您可以非常干净地链接它:

Post.for_user(user).last_created
当然,您也可以链接方法,但如果您处理的是查询接口方法,我觉得作用域更有意义,而且更干净

如果您想测试它是否返回正确的记录,您可以在测试中执行以下操作:

let!(:last_created_post) { factory_to_create_post }

. . . 

it "returns the correct post"
  expect(Post.last_post).to eq(last_created_post)
end

如果您想要有一个更好的测试,您可以在最后一个记录之前创建一对记录,以验证被测试的方法是否提取了正确的结果,而不仅仅是单个记录的结果。

接受的答案是不正确的。只需执行
Post.last
即可按
ID
排序,而不是按创建时间排序

如果您使用的是顺序ID(理想情况下不应该这样),那么显然这会起作用,但如果不是,则需要指定排序依据的列。因此,要么:

def self.last_post
  order(created_at: :desc).first
end
或:

就我个人而言,我希望将其作为一个范围,而不是一个专门的方法

scope :last_created -> { order(:created_at).last }
这允许您使用其他作用域创建一些不错的链接,例如,如果您有一个特定用户/帐户查找所有帖子,那么您可以非常干净地链接它:

Post.for_user(user).last_created
当然,您也可以链接方法,但如果您处理的是查询接口方法,我觉得作用域更有意义,而且更干净

如果您想测试它是否返回正确的记录,您可以在测试中执行以下操作:

let!(:last_created_post) { factory_to_create_post }

. . . 

it "returns the correct post"
  expect(Post.last_post).to eq(last_created_post)
end

如果您想进行更好的测试,可以在最后一条记录之前创建一对记录,以验证被测试方法是否提取了正确的结果,而不仅仅是单个记录的结果。

使用
。没有任何指定列的最后一条
将默认为在模型的ID列上排序,这意味着只有在使用顺序ID的情况下(理想情况下不应该使用顺序ID),这才有效。在没有任何指定列的情况下使用
.last
将默认为在模型的ID列上排序,这意味着只有在使用顺序ID时(理想情况下不应该使用顺序ID),这才有效。