首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rspec模型测试;方法无效

rspec模型测试;方法无效
EN

Stack Overflow用户
提问于 2016-04-16 11:58:40
回答 2查看 177关注 0票数 0

该方法本身正常工作,但是当我试图在模型规范中测试它时,create部件失败了。找到的部分很好用。我错过了什么?

conversation.rb

代码语言:javascript
复制
scope :between, -> (sender_id, recipient_id) do
  where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", sender_id, recipient_id, recipient_id, sender_id)
end

def self.create_or_find_conversation(task_assigner_id, task_executor_id)
  Conversation.between(task_assigner_id, task_executor_id).first_or_create do |conversation|
    conversation.sender_id = task_assigner_id
    conversation.recipient_id = task_executor_id
  end
end

conversation_spec.rb

代码语言:javascript
复制
describe "class methods" do

  let(:sender) { create(:user) }
  let(:recipient) { create(:user) }
  let(:other_recipient) { create(:user) }
  let!(:conversation) { create(:conversation, sender: sender, recipient: recipient) }

  context "create_of_find_conversation" do

   #this one throws Failure/Error: expect{conv}.to change{Conversation.count}.by(1)
   #expected result to have changed by 1, but was changed by 0
    it "creates conversation" do
      conv = Conversation.create_or_find_conversation(sender, other_recipient)
      expect{conv}.to change{Conversation.count}.by(1)
    end

    #this one is working as expected
    it "finds conversation" do
      conv = Conversation.create_or_find_conversation(sender, recipient)
      expect(conv).to eq(conversation)
    end
  end
end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-16 13:52:48

我认为这些代码:

代码语言:javascript
复制
it "creates conversation" do
  conv = Conversation.create_or_find_conversation(sender, other_recipient)
  expect{conv}.to change{Conversation.count}.by(1)
end

应改为:

代码语言:javascript
复制
it "creates conversation" do
  expect{
      Conversation.create_or_find_conversation(sender.id, other_recipient.id)
  }.to change{Conversation.count}.by(1)
end

因为它不是值改变了计数,而是进程。

票数 3
EN

Stack Overflow用户

发布于 2016-04-16 16:18:11

Ruby中的正则变量不是延迟加载--在分配变量时,分配的右侧将被处理。

代码语言:javascript
复制
def do_something(val)
  puts "do_something called"
  val
end

a = do_something(hello_world)
puts a
# do_something called
# hello world

您要么需要更改期望,以便在传递的块中调用该操作以期望:

代码语言:javascript
复制
it "creates conversation" do
  expect do
    Conversation.create_or_find_conversation(sender, other_recipient)
  end.to change{Conversation.count}.by(1)
end

或者使用RSpec的let创建一个延迟加载变量:

代码语言:javascript
复制
let(:conv) { Conversation.create_or_find_conversation(sender, other_recipient) }

it "creates conversation" do
  expect { conv }.to change{Conversation.count}.by(1)
end

但这并不能解决您对域建模错误的根本问题。在一次谈话中,双方轮流交谈--所以使用recipient_idsender_id是完全错误的。相反,它的消息有发送方和接收方。

您可以将它们更改为任意名称,但是使用适当的多到多关系要简单得多,这样就不需要复杂的AND OR查询了。

代码语言:javascript
复制
class User < ActiveRecord::Base
  has_many :user_conversations
  has_many :conversations, through: user_conversations
end

class Conversation < ActiveRecord::Base
  has_many :user_conversations
  has_many :users, through: user_conversations
end

# the m2m join model.
class UserCoversation < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
end

然后,您可以简单地查询:

代码语言:javascript
复制
Conversation.where(users: [a, b])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36663895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档