进行简单测试:
context "in the same board" do
@link = FactoryGirl.create(:link_with_board, url: "www.onet.pl")
@board = @link.board
it "is invalid when the same url already exists" do
expect(@board.links.build(url: "www.onet.pl")).to_not be_valid
expect(@board.links.build(url: "http://www.onet.pl")).to_not be_valid
expect(@board.links.build(url: "www.onet.pl/")).to_not be_valid
end
end
它向我显示错误:
Failures:
1) Link in the same board is invalid when the same url already exists
Failure/Error: expect(@board.links.build(url: "www.onet.pl")).to_not be_valid
NoMethodError:
undefined method `links' for nil:NilClass
# ./spec/models/link_spec.rb:50:in `block (3 levels) in <top (required)>'
当我在控制台中尝试相同的操作时,一切正常。知道为什么吗?
更新:
好的,我让它工作了,但问题仍然存在,为什么第一个不起作用?
context "in the same board" do
#FIX START
before :each do
@link = FactoryGirl.create(:link_with_board, url: "www.onet.pl")
@board = @link.board
end
#FIX END
it "is invalid when the same url already exists" do
expect(@board.links.build(url: "www.onet.pl")).to_not be_valid
expect(@board.links.build(url: "http://www.onet.pl")).to_not be_valid
expect(@board.links.build(url: "www.onet.pl/")).to_not be_valid
end
end