Solved

UnitTest ADO.Net Entity Framework

Posted on 2011-02-17
3
729 Views
Last Modified: 2012-05-11
Hi!

Having some problem with my UnitTest, I'm trying to test my Add (INSERT) method, but it does not behave the way I want.

I have a Store Procedure that executes the INSERT statement, I have done all the mapping and it works fine. The first insert works, it inserts the values as it should. When I run it again with the same FolderPath the store procedure should NOT INSERT it  again, that works fine too, but still I get 1 returned from ctx.SaveChanges() method, but It  should return 0?


[TestMethod]  
public void AddTest()  
{  
    // Arrange  
    IRepository<FileSystemWatcher> repository = new FileSystemWatcherRepository();  
  
    FileSystemWatcher entity = new FileSystemWatcher() { FolderPath = @"C:\Test" };  
  
    // Act  
    bool actual = repository.Add(entity);  
  
    // Assert  
    Assert.IsTrue(actual);  
}  
  
public bool Add(FileSystemWatcher entity)  
{  
    // Get Entity  
    using (var ctx = new FbiDmsEntities())  
    {  
        // Add object to entity  
        ctx.AddToFileSystemWatcher(entity);  
  
        // Commit  
        int result = ctx.SaveChanges();  
  
        // Affected rows  
        if (result == 1)  
        {  
            return true;  
        }  
        else  
        {  
            return false;  
        }  
    }  
}  
  
Use [FBI.DMS];   
GO  
SET ANSI_NULLS ON;  
GO  
SET QUOTED_IDENTIFIER ON;  
GO  
SET NOCOUNT ON;  
GO        
-- ====================================================  
-- Author:                
-- Create date: 2011-02-13  
-- Description: Insert a folder path  
-- ====================================================  
IF OBJECT_ID('InsertFileSystemWatcher') IS NOT NULL  
        DROP PROC InsertFileSystemWatcher;  
GO  
  
CREATE PROCEDURE InsertFileSystemWatcher  
(  
        @FolderPath varchar(300)  
)  
AS  
DECLARE @Id int;  
  
BEGIN TRANSACTION FileSystemWatcherInsert  
    WITH MARK N'Inserting a file system watcher folder path';  
      
BEGIN                  
        -- Check so this folder path already exists  
        SELECT @Id = [FileSystemWatcherId]  
                   
        FROM dbo.[FileSystemWatcher]  
          
        WHERE   
                [FolderPath] = @FolderPath;                 
END;  
  
BEGIN  
        --State 1: Do not insert if the folder path already exists, raise error  
        IF      @Id IS NULL  
                BEGIN  
                        -- INSERT folder path             
                        INSERT   
                           
                        INTO dbo.[FileSystemWatcher]  
                        (  
                                [FolderPath]  
                        )  
                        VALUES  
                        (  
                                @FolderPath  
                        );                        
                END;  
        ELSE  
                BEGIN  
                        RAISERROR ('The record already exists in the database.', 1, 1);   
                END;              
END;  
          
-- commit the transaction  
COMMIT TRANSACTION FileSystemWatcherInsert;  
GO  
  
  
-------------------------------------  
Table  
-------------------------------------  
USE [FBI.DMS]  
GO  
  
/****** Object:  Table [dbo].[FileSystemWatcher]    Script Date: 02/17/2011 08:48:35 ******/  
SET ANSI_NULLS ON  
GO  
  
SET QUOTED_IDENTIFIER ON  
GO  
  
CREATE TABLE [dbo].[FileSystemWatcher](  
        [FileSystemWatcherId] [int] IDENTITY(1,1) NOT NULL,  
        [TimeStamp] [datetime] NOT NULL,  
        [FolderPath] [nchar](300) NOT NULL,  
 CONSTRAINT [PK_FileSystemWatcherSettings] PRIMARY KEY CLUSTERED   
(  
        [FileSystemWatcherId] ASC  
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  
) ON [PRIMARY]  
  
GO  
  
ALTER TABLE [dbo].[FileSystemWatcher] ADD  CONSTRAINT [DF_FileSystemWatcherSettings_TimeStamp]  DEFAULT (getdate()) FOR [TimeStamp]  
GO

Open in new window

0
Comment
Question by:ulf-jzl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 34918819
The method is doing returning what it is supposed to. Here's the MSDN page for SaveChanges().


It describes the return value as being:

"The number of objects in an Added, Modified, or Deleted state when SaveChanges was called."

So it's returning the correct thing. You had one object in an Added state when SaveChanges was called. It doesn't have anything to do with how many rows in the database were affected.
0
 

Author Comment

by:ulf-jzl
ID: 34920927
Ok, thx man. I will start a new question for solving the problem, returning affected rows.
0
 

Author Comment

by:ulf-jzl
ID: 34921186
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question