我正在编写Rspec requests spec
,在此之前我想使用FactoryBot
构建一些测试数据。
现在我有一个模型Game
:
class Game < ApplicationRecord
has_many :game_levels
和模型GameLevel
:
class GameLevel < ApplicationRecord
belongs_to :game
在我的/spec/factories/game.rb
中:
FactoryBot.define do
factory :game do
name { :Mario }
end
end
在我的spec/factories/game_level.rb
中:
FactoryBot.define do
factory :game_level do
name { :default }
min_level { 0 }
max_level { 100 }
game
end
end
在我的spec/requests/user_plays_game_spec.rb
中,我只编写了代码来创建game&game_level,然后打印了game.id
,game_level.game_id
。我发现它们不一样。此外,game.game_levels
返回nil
。
before(:all) do
@game = create(:game)
@game_level = create(:game_level)
end
describe do
it do
puts @game,id, @game_level.game_id
puts @game.game_levels
expect(@game.id).to eql(@game_level.game_id)
end
end
那么如何使用belongs_to
将has_many
记录与FactoryBot
记录关联?
答案 0 :(得分:5)
您可以在创建过程中将其关联
extension ViewController : FetchedResultsControllerManagerDelegate {
func managerDidChangeContent(_ controller: NSObject, change: FetchedResultsControllerManagerChange) {
change.applyChanges(collectionView: collectionView)
}
}
答案 1 :(得分:0)
工厂协会很直观:
# Loading data
data(mtcars)
# Manual transmission (am = 1) depends on 1/4 mile time (qsec) and miles/(US) gallon (mpg)
glmfit <- glm(am ~ qsec + mpg, data = mtcars, binomial)
mtcars$fitted_am <- glmfit$fitted.values
# Loading packages
library(plotROC)
library(ggplot2)
library(pROC)
# Calculating ROC curve, AUC and threshold according to Youden index
rocfit <- roc(mtcars$am, mtcars$fitted_am)
auc(rocfit)
coords(rocfit, x = "b")
basicplot <- ggplot(mtcars, aes(d = am, m = fitted_am))
basicplot +
geom_roc() +
style_roc(theme = theme_grey) +
theme(axis.text = element_text(colour = "blue")) +
ggtitle("Automatic transmission prediction") +
scale_x_continuous("1 - Specificity", breaks = seq(0, 1, by = .1))
plot(rocfit)
prop.table(table(mtcars$am))
没有幻想。如果它不适合您,则可能是配置问题。