Reasonably green to testing but I was following along with a simple udemy course. I used the RSpec documentation to set up RSpec in rails to try out some testing. But I have come across an issue that for the life of me I can't figure out...
`require "rails_helper"
RSpec.describe User, type: :model do
subject { described_class.new("John") }
it "initializes a name" do
expect(subject.name).to eq("John")
end
context "with no argument" do
subject { described_class.new }
it "should default to Bill as the name" do
expect(subject.name).to eq("Bill")
end
end
end`
This is my test code.
This is my User model.
class User < ApplicationRecord
attr_reader :name
def initialize(name = "Bill")
@name = name
end
end
When I run the test it fails and says that the second test isn't returning Bill but 'nil'. However, in my User model if I remove the < Application Record it passes... Also, if I add a second parameter in the initialize, it randomly passes the default test and fails the first one returning the default name... I'm completely confused as I have been learning the testing without ApplicationRecord and that seems to be the part where it is failing. I have tried changing the subject to let(:testing){User.new} but that doesn't work. Any help seriously appreciated here as I can't seem to find it through google.
To let you know I have gem 'rspec-rails', '~> 4.0.0' included in my GemFile in the :development, :test part.
Aucun commentaire:
Enregistrer un commentaire