我想在Rspec
中将单元测试写入课程:
class Item < ActiveRecord::Base
has_many :cart_items
has_many :carts, through: :cart_items
validates :price, presence: true
validates :name, presence: true
end
我不确定我应该在这里测试一下......你能给出一些提示吗?
答案 0 :(得分:1)
使用shoulda-matchers gem,你可以这样做
describe Item do
it {should validate_presence_of :price}
it {should validate_presence_of :name}
it {should have_many(:cart_items)}
it {should have_many(:carts).through(:cart_items)}
end
答案 1 :(得分:1)
在spec/models/item_spec.rb
中你可以编写单元测试。
我只是给你一个例子,你应该在你的特定模型的规范中写下什么
require 'spec_helper'
describe Task do
before do
@task = Task.new(name: "Example1", price:123)
end
subject { @task }
it { should respond_to(:name) }
it { should respond_to(:price) }
it { should respond_to(:cart_items) }
it { should be_valid }
describe "when name is not present" do
before { @task.name = " " }
it { should_not be_valid }
end
end
您可以根据验证,要求,字段,关联等编写更多内容