Ruby on rails rails rspec在测试时访问其他表
我对rails非常陌生,发现自己在理解数据库表之间的关系方面遇到了一些问题(我猜是吧?) 我的问题如下:Ruby on rails rails rspec在测试时访问其他表,ruby-on-rails,ruby,rspec,rspec-rails,Ruby On Rails,Ruby,Rspec,Rspec Rails,我对rails非常陌生,发现自己在理解数据库表之间的关系方面遇到了一些问题(我猜是吧?) 我的问题如下: require 'spec_helper' describe Game do before do @game = Game.new( player1: "foobar@example.com", faction1: "Some faction", caster1: "Some cast
require 'spec_helper'
describe Game do
before do
@game = Game.new( player1: "foobar@example.com",
faction1: "Some faction",
caster1: "Some caster",
player2: "bazbaz@example.com",
faction2: "Some other faction",
caster2: "Some other caster",
points: 35,
won: true)
end
...
(some other specs)
...
describe "saving a game" do
let(:player1) { User.create(name: "Example1",
email: "example1@foo.bar",
password: "foobar",
password_confirmation: "foobar") }
let(:player2) { User.create(name: "Example2",
email: "example2@foo.bar",
password: "foobar",
password_confirmation: "foobar") }
describe "with invalid players" do
describe "when both players do not exist" do
before { @game.player1 = @game.player2 = "some_wrong_user@example.com" }
it { should_not be_valid }
end
describe "when player1 does not exist" do
before { @game.player2 = player2 }
it { should_not be_valid }
end
describe "when player2 does not exist" do
before { @game.player1 = player1 }
it { should_not be_valid }
end
end
describe "with valid players" do
before do
@game.player1 = player1
@game.player2 = player2
end
it { should be_valid }
end
end
end
我有一个用户表,包含用户的信息,包括他们的电子邮件地址,还有一个表包含我想管理那些玩家玩的“游戏”。
当玩家想要提交游戏时,他们必须通过电子邮件地址指定参与游戏的用户。我想验证这些玩家是否真的存在
我的游戏模型:
class Game < ActiveRecord::Base
attr_accessible :caster1, :caster2, :faction1, :faction2, :player1, :player2, :points, :won
before_save { |game| player1 = player1.downcase }
before_save { |game| player2 = player2.downcase }
validate :existence_of_both_players
...
(some more validations)
...
private
def existence_of_both_players
User.exists?(:email => :player1.downcase) && User.exists?(:email => :player2.downcase)
end
end
(很抱歉代码太多,我只是觉得这会有帮助)
我的考试不及格,我很确定这是显而易见的原因,不幸的是,对我来说不是
Failures:
1) Game saving a game with invalid players when both players do not exist
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/game_spec.rb:108:in `block (5 levels) in <top (required)>'
...
故障:
1) 当两个玩家都不存在时,使用无效玩家保存游戏
失败/错误:它{应该是无效的}
预期有效?返回假,得到真
#./spec/models/game_spec.rb:108:in'block(5级)in'
...
我真的不明白,为什么这不起作用。我读了rails的书,看了几段视频,但没有一段能很好地解释我的问题
由于这是我在stackoverflow上的第一篇文章,如果这篇文章太冗长,请让我知道。提前感谢。自定义验证程序方法应调用errors.add以发出错误信号。因此,在您的情况下:
def existence_of_both_players
player_one_exists = User.find_by_email :player1.downcase
player_two_exists = User.find_by_email :player2.downcase
unless player_one_exists and player_two_exists
errors.add :game, "both players need to exist"
end
end
请阅读指南中的更多信息 你在游戏模型中将玩家设置为仅使用电子邮件地址,而不是使用关联,这有什么原因吗。主要原因是,我真的不知道如何与协会合作,因为我通常不是一个数据库专家。我的想法是,用户最容易记住并填写某人的电子邮件地址。这是我第一次和他们一起工作。你有什么建议吗?这是一个比较好的初学者rails问题,通常都很糟糕。
player1=
不起作用,替换为:self.player1=
@user1685739我建议要么向用户添加施法者和阵营,要么创建一个中间模型(玩家?)如果这些值有一个:user,那么您的游戏就有多个用户/玩家,还有一个:winner。抱歉,这有点复杂,但是如果你将这些设置为关联,你将使你的生活更轻松,因为你可以更容易地引用游戏的玩家和用户的游戏。当用户输入相同的电子邮件地址时,您可以验证该电子邮件地址。根据,exists
需要一个id
true-更改为通过电子邮件查找。\u非常感谢,这段代码修复了其中一个问题。谢谢你的链接,总是很高兴有更多的学习资源。