我是Rspecs的新手,所以我写的以下代码是正确的......我使用过简单的Rspec。目前Rspec运行正常,但是客户预测需要做哪些更改?
如果有人可以查看我的代码并帮助我,我将不胜感激....
require 'spec_helper'
require 'rails_helper'
describe User do
describe "fails" do
it "to create without email" do
user = User.create(:first_name => "test", :last_name => "test",:email => "")
user.errors.messages[:email][0].should eq "can't be blank"
end
end
describe "fails" do
it "to create without data" do
user = User.create(:first_name => "", :last_name => "", :email => "", :status => "")
user.errors.messages[:email][0].should eq "can't be blank"
end
end
describe "fail" do
it "to create without a valid email" do
user = User.create(:first_name => "", :last_name => "", :email => "test@test1", :status => "")
expect(user).to be_valid, lambda { "expected valid email" }
end
end
describe "pass" do
it "to supports 2 letter domain qualifiers" do
user = User.create(:first_name => "", :last_name => "", :email => "test@test1.ca", :status => "")
user.save!
end
end
describe "pass" do
it "to supports 4 letter domain qualifiers" do
user = User.create(:first_name => "", :last_name => "", :email => "test@test1.some", :status => "")
user.save!
end
end
end
describe "pass" do
it "to supports 4 and more letter domain qualifiers" do
user = User.create(:first_name => "", :last_name => "", :email => "test@test1.super", :status => "")
user.save!
end
end
describe "pass" do
it "to without first name" do
user = User.new
user.first_name = ""
user.last_name = "Test"
user.status = "active"
user.email = "test127@test.com"
user.save!
end
end
describe "pass" do
it "to without last name" do
user = User.new
user.first_name = "Test"
user.last_name = ""
user.status = "active"
user.email = "test127@test.com"
user.save!
end
end
describe "pass" do
it "to without status" do
user = User.new
user.first_name = "Test"
user.last_name = "Test"
user.status = ""
user.email = "test127@test.com"
user.save!
end
end
describe "pass" do
it "to create new record without first name, last name and status" do
user = User.create(:first_name => "", :last_name => "", :email => "test@test1.com", :status => "")
user.save!
end
end
describe "pass" do
it "to create new record with all parameter" do
user = User.new
user.first_name = "Test"
user.last_name = "Test"
user.status = "active"
user.email = "test127@test.com"
user.save!
end
end
describe "pass" do
it "to update record" do
user = User.find_by_id(1127)
user.first_name = "Test"
user.last_name = "Test"
user.status = "active"
user.email = "test127@test.com"
user.save!
end
end
答案 0 :(得分:0)
spec_helper
和rails_helper
。 spec_helper
中已经需要rails_helper
。 spec_helper
用于不依赖于Rails的规范(例如lib目录中的类)。 rails_helper
适用于依赖于Rails的规范(例如您的模型和控制器)--format documentation
标志运行您的规范,以查看您的示例和上下文名称是否有意义。在这种情况下,我相信你可以改进它。而不是
describe "fails" do
it "to create without email" do
...
end
end
写
更有意义describe "when creating user without email" do
it "adds error" do
...
end
end
等等。您还可以添加does not create new record
您的pass
示例根本没有测试任何内容。而不是写这个
user = User.create(...) user.save!
你可以写
expect { User.new(...).save }.to change(User, :count).by(1)
当你使用工厂时,我确信正确的规格应该是这样的
require 'rails_helper'
describe User do
let(:user) { FactoryGirl.build :user, first_name: 'First name', last_name: 'Last name', email: 'email@gmail.com' }
context 'when user want to create account without email' do
before { user.email = nil }
it 'does not save user record' do
expect { user.save }.not_to change(User, :count)
end
end
end
答案 1 :(得分:0)
it "fails without email" do
user = User.create(:first_name => "test", :last_name => "test",:email => "")
user.errors.messages[:email][0].should eq "can't be blank"
end
it "fails without parameters" do
user = User.create(:first_name => "", :last_name => "", :email => "", :status => "")
user.errors.messages[:email][0].should eq "can't be blank"
end
it "fails without a valid email" do
user = User.create(:first_name => "", :last_name => "", :email => "test@test", :status => "")
expect(user).to be_valid, lambda { "expected valid email" }
end
it "passes without first name" do
pre_count = User.count
user = User.new
user.first_name = ""
user.last_name = "Test"
user.status = "active"
user.email = "test127@test.com"
user.save!
count = User.count
tot_count = pre_count + 1
count.should be tot_count
user.email.should eq "test127@test.com"
user.first_name.should eq nil
user.last_name.should eq "Test"
user.status.should eq :active
end