ruby-on-rails - rspec 测试 getter 和 setter 方法

标签 ruby-on-rails testing rspec getter-setter

任务.rb

#executor (user class) has one profile and has many tasks.

def task_name_company
  [executor.try(:profile).try(:first_name), executor.try(:profile).try(:last_name), executor.try(:profile).try(:company)].join(' ')
end

def task_name_company=(name)
  self.executor = User.joins(:profile).where("CONCAT_WS(' ', first_name, last_name, company) LIKE ?", "%#{name}%").first if name.present?
rescue ArgumentError
  self.executor = nil
end

任务规范.rb

let(:task) = { create(:task) }
let(:user) = { create(:user) }
let(:profile = { create(:profile) }


# I tested the getter like this and seems to be working
it "task_name_company getter" do
  task.executor.profile = profile
  expect(task.task_name_company).to eq("John Doe ABC Inc")
end

# I don't know how to test the setter method

it "task_name_company setter" do
end

更新 LET VS LET!

用户.rb

def decrease_new_chat_notifications
  decrement!(:new_chat_notification) if self.new_chat_notification > 0
end

def decreasing_post_notification_number(post_id)
  notifications.this_post_comments(post_id).unchecked.each do |notification|
    checking_and_decreasing_notification(notification)
  end
end

用户规范.rb

let(:user) { create(:user) }
let(:post_notification) { create(:notification, notifiable_type: "Post", recipient: user, sender: sender, checked_at: nil, notifiable_id: post.id)}

it "decrease_new_chat_notifications if greater than 0" do
  user.new_chat_notification = 1
  expect{user.decrease_new_chat_notifications}.to change{user.new_chat_notification}.by(-1)
end

it "decreasing_post_notification_number" do
  expect(user).to receive(:checking_and_decreasing_notification).with(post_notification)
  user.decreasing_post_notification_number(post.id)
end

最佳答案

像这样:

describe '#task_name_company=' do
  let(:profile) { create(:profile, first_name: 'name', last_name: 'last', company: 'company') }
  let!(:user) { create(:user, profile: profile) }
  subject { create(:task) }

  it 'sets the executor' do
    subject.task_name_company = 'name last company'
    expect(subject.user).to eql user
  end
end

更新 以回答 let!let

describe '#task_name_company=' do
  let(:profile) { create(:profile, first_name: 'name', last_name: 'last', company: 'company') }
  let(:user) { create(:user, profile: profile) }
  subject { create(:task) }

  it 'sets the executor' do
    user # I'm explicitly calling user here
    subject.task_name_company = 'name last company'
    expect(subject.user).to eql user
  end
end

关于ruby-on-rails - rspec 测试 getter 和 setter 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36653806/

相关文章:

ssh - 无法远程运行针对 Safari 11 的测试(ssh、CI)

ruby - 从 FactoryGirl 更新到 factoryBot 导致 NoMethodError

ruby-on-rails - 使用 AASM 调用状态更改方法

javascript - 如何设置包含属于product_category 的选项的订单

java - RobotFramework:超出启动关键字的最大限制

python - 我应该在每个测试中断言相同的值吗?

c++ - 使用rspec测试C/C++程序

ruby-on-rails - 目录 : undefined method new for #<Class>

ruby-on-rails - 构建匹配链接数组

javascript - 如何使用javascript模拟鼠标点击和鼠标移动