问题描述
我是Ruby,Rails和TDD的新手,在测试过程中遇到了麻烦。 我正在尝试测试由FactoryBot创建的用户发布的食谱模型的创建。
我的食谱模型(app / models / recipe.rb)是:
class Recipe < ActiveRecord::Base
resourcify
validates :image,presence: true
validates :title,presence: true
validates :preparazione,presence: true
belongs_to :user
has_many :likes
has_many :comments
has_one_attached :image
end
在spec / models / recipe_spec.rb中,我有:
require "rails_helper"
RSpec.describe Recipe,type: :model do
describe "Creating a Recipe" do
it "should be permitted" do
@user = FactoryBot.create(:user)
recipe = Recipe.new(title: 'Recipe',preparazione: 'Preparation',image: '../support/test_image.jpg',user_id: @user.id,n_likes: 0,n_comments: 0,created_at: Time.now.utc)
expect(recipe).to be_valid
@user.destroy
end
end
end
此测试失败,错误是:
Failures:
1) Recipe Creating a Recipe should be permitted
Failure/Error: expect(recipe).to be_valid
ActiveSupport::MessageVerifier::InvalidSignature:
ActiveSupport::MessageVerifier::InvalidSignature
# ./spec/models/recipe_spec.rb:16:in `block (3 levels) in <top (required)>'
Finished in 0.35273 seconds (files took 7.93 seconds to load)
7 examples,1 failure
Failed examples:
rspec ./spec/models/recipe_spec.rb:5 # Recipe Creating a Recipe should be permitted
为什么会显示此错误,这是什么意思?
解决方法
def get_data():
source = API_Instance()
yield source.get_some_data()
def parse_data():
data = get_data()
while True:
try:
row_data = next(data)
print(row_data)
except StopIteration:
break
在Rspec中使用Rack::Test::UploadedFile
class上传附件。